Answered CAML Query - DateTime

  • Monday, March 24, 2008 2:27 PM
     
     
    Hello!
    I wish to take data from the list, satisfying to the certain time range (BeginDate.....DateTime.....EndDate). How it can be made?

All Replies

  • Monday, March 24, 2008 3:42 PM
     
     Answered

     

    This CAML Query below return the items based on MyDateColumn with less than 5 days. Basically using a Greator or Equal operatior and providing Today date - 5 days as a filter.
     
    <Query>
        <Where>
            <Geq>
                <FieldRef Name="MyDateColumn" />
                <Value Type="DateTime">
                    <Today OffsetDays="-5" />
                </Value>
            </Geq>
        </Where>
    </Query>
     
     
    You can use greator or equal like above and less or equal filter with AND operator in CAML above to work with date range. 
     
    If the date value is coming from your variable etc then you need to use SPUtility function and get the string required by CAML for date time variable.
     
       DateTime CustomDate = DateTime.Now;
                string strCustomDate  = SPUtility.CreateISO8601DateTimeFromSystemDateTime(CustomDate); 
     
     
  • Friday, October 24, 2008 1:18 PM
     
     Proposed Has Code
    Hi,
    I have a question.
    Can caml works with hours and minutes too ?
    Because my doesn't :-(

    I built this query


    DateTime start = Convert.ToDateTime(_properties.AfterProperties["start"].ToString()); 
    DateTime end = Convert.ToDateTime(_properties.AfterProperties["end"].ToString()); 
     
     
                    SPQuery query1 = new SPQuery(); 
                    query1.Query = "<Where>"
                                       "<And>" + 
                                           "<Eq>" + //Equal to 
                                             "<FieldRef Name='reason'></FieldRef>" + 
                                             "<Value Type='Text'>" + whatIsbooking + "</Value>" + 
                                            "</Eq>" + 
                                         "<Or>" + 
                                                "<And>" +  
                                                "<Geq>" + // Greater than or euqual to 
                                                    "<FieldRef Name='end'></FieldRef>" + 
                                                    "<Value Type='DateTime'>" + SPUtility.CreateISO8601DateTimeFromSystemDateTime(end) + "</Value>" + 
                                                "</Geq>" + 
                                                "<Leq>" + //less then or equal to 
                                                    "<FieldRef Name='start'></FieldRef>" + 
                                                    "<Value Type='DateTime'>" + SPUtility.CreateISO8601DateTimeFromSystemDateTime(end) + "</Value>" + 
                                                "</Leq>" + 
                                              "</And>" + 
                                              "<And>" +  
                                                "<Leq>" +  
                                                    "<FieldRef Name='start'></FieldRef>" + 
                                                    "<Value Type='DateTime'>" + SPUtility.CreateISO8601DateTimeFromSystemDateTime(start) + "</Value>" + 
                                                "</Leq>" + 
                                                "<Geq>" +  
                                                    "<FieldRef Name='end'></FieldRef>" + 
                                                    "<Value Type='DateTime'>" + SPUtility.CreateISO8601DateTimeFromSystemDateTime(start) + "</Value>" + 
                                                "</Geq>" + 
                                            "</And>" + 
                                         "</Or>" + 
                                       "</And>"
                                   "</Where>"

    It checks just days, not hours or minutes.
    • Proposed As Answer by Musta2 Friday, October 24, 2008 1:22 PM
    • Edited by Musta2 Friday, October 24, 2008 1:23 PM
    •  
  • Thursday, April 23, 2009 6:33 AM
     
     Proposed
    Hi ,
    To include time in DateTime Query, you have to set IncludeTimeValue=”TRUE”.
     
    Ex: <Value Type='DateTime' IncludeTimeValue='True'></Value>

    Hope this helps :) .
    • Proposed As Answer by Tester Thursday, April 23, 2009 6:33 AM
    •  
  • Thursday, January 21, 2010 3:10 PM
     
     
    buddy , check this out whole syntax including how to get the right format of datetime for CAML query http://muzms.blogspot.com/2010/01/datetime-value-comparison-in-caml-query.html
  • Tuesday, January 26, 2010 2:09 PM
     
     
    This was very useful information - I was banging my head on this all day. The SPUtility method formatted the string correctly, but I needed to format the string without using SharePoint utilities. When I looked through the standard formats for the ToString method, I found this option:

    string camlQueryString = DateTime yourSampleDate.ToString("s");

    When I executed the query on SharePoint, it also worked just fine!
  • Friday, February 19, 2010 3:48 PM
     
     Proposed

    Hello

    The solution is already proposed by native CAML Query and  confirmed by "Tester"

    I normally use something like that :

     <Query>
        <Where>
             <Leq>
            <FieldRef Name='BeginDate' />
            <Value Type='DateTime'  IncludeTimeValue='TRUE'><Today /></Value>
            </Leq>
        <Where>
    </Query>

    Same will apply to  EndDate, etc

    Hope it helps Cheers :)

     


    Momo
    • Proposed As Answer by Mohamed Hachem Friday, February 19, 2010 3:48 PM
    •  
  • Tuesday, September 21, 2010 10:52 AM
     
     
  • Friday, June 22, 2012 9:23 AM
     
     

    Hello,

    <OrderBy><FieldRef Name='CreateDate' Ascending='False' /></OrderBy>

    I am not able to apply Order by on Date Column using CAML Query. Can anyone help how to order by in date column?

    Thanks in advance

  • Wednesday, July 18, 2012 8:07 PM
     
     

    <OrderBy><FieldRef Name='CreateDate' Type='DateTime' Ascending='False' /></OrderBy>

    Add Type='DateTime' attribute.