Trouble using SOQL to filter results by a range of Dates – What is the correct syntax?

Using the Salesforce API, I am trying to search for Cases which have a CreatedDate between two specific dates, but I am getting errors when sending my query.

For example:

SELECT Id FROM Case  
WHERE  CreatedDate >= 2013-12-21  AND  CreatedDate <= 2013-12-23

The error I get is:

value of filter criterion for field 'CreatedDate' 
must be of type dateTime and should not be enclosed in quotes

I read the “Date Formats and Date Literals” article and do not see where the issue is in my query.

Answer

CreatedDate is of DateTime type so your query must use the format from the very top of the article you’ve linked to: YYYY-MM-DDThh:mm:ssZ (optionally with timezones)

SELECT Id 
FROM Case  
WHERE CreatedDate >= 2013-12-21T00:00:00Z
AND CreatedDate <= 2013-12-23T23:59:59Z

Another example:

SELECT Id
FROM Account
WHERE CreatedDate > 2005-10-08T01:02:03Z

Attribution
Source : Link , Question Author : Jdinklage Morgoone , Answer Author : Eric Leschinski

Leave a Comment