Sunday, October 16, 2011

How to query using date time field in Salesforce

There are many instances where we need to query on base of createddate or any other datetime field to find some specific records for particular day or for that month or for that year etc.

We cannot directly query with datetime field in where condition.

So you will need to use Date functions in SOQL to query using datetime field in where condition

Example:
 Opportunity =[SELECT Id, Name FROM Opportunity WHERE DAY_ONLY(CreatedDate)= '2011-10-16'];  


Refer other date functions which are available so that you can query easily using datefield in where condition.
Date functions