Using a date for a datetime field in a SOQL Query

CreatedDate is a datetime field so I guess you would need to specify the time component.

Can you do something like where you just append the time portion to be 12 am by default.

WHERE CreatedDate > 2005-10-08T00:00:00Z

Or you can use Date Literals like

 WHERE CreatedDate > YESTERDAY

For more on date formats and more literal values see,

http://www.salesforce.com/us/developer/docs/soql_sosl/Content/sforce_api_calls_soql_select_dateformats.htm


To filter a SOQL query on a datetime field with a day value, use the DAY_ONLY SOQL operator.

SELECT Id
FROM Account
WHERE DAY_ONLY(CreatedDate) > 2005-10-08

You can bind a date variable into the SOQL Query as well, but the results might not be what you expect as there the date variable would be cast into a DateTime type at run time.


If you want to avoid date manipulation with Apex, you could also create a custom date formula field and query off that field. Your formula would look like this:

DATEVALUE(CreatedDate)

Then just query off your custom field.

Let me know if you have any questions.

Tags:

Soql