5
votes

I'm trying to put together a CAML Query that compares two DateTime objects, but I cannot get it to work using an Eq comparison. From my testing I can get Gt, Lt, Geq, Leq to work with DateTime comparisons, but Eq doesn't seem to work at all, ever.

The first object is a Date and Time field (produced by InfoPath and saved to a Date and Time field in a SharePoint list), the current example has "3/14/2012 12:00 AM". I have attempted using the [Today /] value, using a hard-coded value in ISO format 2012-03-14T00:00:00Z but nothing has worked so far. I have experimented with IncludeTimeValue, setting it to true/false, no improvement.

My current query looks a little like this,

<Query>
 <Where>
  <Eq>
   <FieldRef Name="SomeDateTimeField" IncludeTimeValue="TRUE" />
   <Value Type="DateTime" IncludeTimeValue="TRUE">2012-03-14T00:00:00Z</Value>
  </Eq>
 </Where>
</Query>

This returns nothing, even though I have an item with that date time in the list. Any ideas?

2
I think in the meantime I'm going to just check if it's in the Gt/Lt range to determine if it's a specific date rather than using Eq to do that. Kind of makes for an uglier query, but it works so far.ferr
You have to remove the quotes around the value.ktharsis
Sorry that was a typo in the post, actual code doesn't reflect that.ferr

2 Answers

3
votes

Equal statement can't return anything, because seconds are counted. Try using date range. Sample:

<Where>
 <And>
  <Gt>
   <FieldRef Name='Created' />
   <Value IncludeTimeValue='TRUE' Type='DateTime'>2014-12-10T00:00:00Z</Value>
  </Gt>
  <Lt>
   <FieldRef Name='Created' />
   <Value IncludeTimeValue='TRUE' Type='DateTime'>2014-12-10T23:59:59Z</Value>
  </Lt>
 </And>
</Where>

Please, pay attention, we use same date, but different time.

2
votes

Does this work:

<Query><Where><Eq><FieldRef Name="SomeDateTimeField"/><Value IncludeTimeValue='TRUE' Type='DateTime'>2012-03-14T00:00:00</Value></Eq></Where></Query>