5

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?

Fabiano
  • 5,124
  • 6
  • 42
  • 69
ferr
  • 1,137
  • 3
  • 12
  • 28
  • 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 Mar 14 '12 at 19:07
  • You have to remove the quotes around the value. – ktharsis Mar 19 '12 at 15:47
  • Sorry that was a typo in the post, actual code doesn't reflect that. – ferr Mar 23 '12 at 12:47

2 Answers2

3

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.

Fabiano
  • 5,124
  • 6
  • 42
  • 69
2

Does this work:

<Query><Where><Eq><FieldRef Name="SomeDateTimeField"/><Value IncludeTimeValue='TRUE' Type='DateTime'>2012-03-14T00:00:00</Value></Eq></Where></Query>
TroyBramley
  • 584
  • 4
  • 7
  • 1
    Because it will return items where field "SomeDateTimeField" = 2012-03-14T00:00:00. Result will be empty If field value different more than one second. – Alexander Ulmaskulov Mar 28 '17 at 15:10