Question

I have a list with a DateTime field.

I want to select the items within a range.

I use this code

<Where>  
 <And>
  <Geq>
    <FieldRef Name='SendDate' /> <Value IncludeTimeValue='TRUE' Type='DateTime'>8/3/2012 12:00:00 AM</Value>
  </Geq>
  <Leq>
    <FieldRef Name='SendDate' />
    <Value IncludeTimeValue='TRUE' Type='DateTime'>8/5/2012 12:00:00 AM</Value> 
  </Leq>
 </And>
</Where>

The list actually has items within this range but it doesn't return any of them.

Was it helpful?

Solution

What is important here is to get the datetime format right. If you use it programatically you could either use this

string sendDate = sendDate.ToString("yyyy-MM-ddTHH:mm:ssZ");

..or you could use the SPUtility.CreateISO8601DateTimeFromSystemDateTime method to achieve the same. Beyond this you could also consider using "relative" expressions based on Today such in (translated into "30 days ago")

 <Geq>
        <FieldRef Name="SendDate" />
<Value Type='DateTime'>
               <Today OffsetDays='-30' />
            </Value>
      </Geq>

OTHER TIPS

Create your query with the U2U Caml Query builder:

http://www.u2u.be/Tools/wincamlquerybuilder/CamlQueryBuilder.aspx

(This works with 2010 as well)

Ideally the following query should work:

<Query>
  <Where>
    <And>
      <Geq>
        <FieldRef Name="SendDate" />
          <Value IncludeTimeValue="TRUE" Type="DateTime">2011-01-01T15:55:52Z</Value>
      </Geq>
      <Leq>
        <FieldRef Name="SendDate" />
        <Value IncludeTimeValue="TRUE" Type="DateTime">2011-12-31T15:56:29Z</Value>
      </Leq>
    </And>
  </Where>
</Query>
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top