Authored by Zishan Razzaq

Wednesday, February 25, 2009

Date Formats and Date Literals in SOQL Query

Hi.. Welcome back..
I always had issues with SOQL Query utilizing dates in particular. So I wanted to share some tips and tricks on writing SOQL queries using some Built in Date Functions...

Now you do your query in 2 ways:

1. Specify a date in your query such as
Select Id,CreatedDate from Account where CreatedDate=DateValue(2008-02-05)
This will return all Id's for all the Account Object that were Created 2008-02-05.

2. Do it with a Fixed Expression or Built in Function such as:
Select Id, CreatedDate from Account where CreatedDate = YESTERDAY
This will return all Id's for the Account Object that were Created yesterday.
This is called a Date Literal.

Now before we go on:
You must know the Syntax for Date in :


For DateTime the Syntax is as follows:

YYYY-MM-DDThh:mm:ss+hh:mm 2009-02-24T23:01:01+01:00
YYYY-MM-DDThh:mm:ss-hh:mm 2009-02-24T23:01:01-08:00
YYYY-MM-DDThh:mm:ssZ 2009-02-24T23:01:01Z

So Lets go into Date Literals: I took this off from the Api docs...
The link is:

Go to search and type in Date Literals... This should come up.(See Below)