No Results
Search Tips:
- Please consider misspellings
- Try different search keywords
Newer Version Available
Date Functions
Date functions allow you to group or filter your data by various date periods. For example, you could use the CALENDAR_YEAR() function to find the sum of the Amount values for all your opportunities for each calendar year.
1swfobject.registerObject("clippy.calendar_yr_sample", "9");SELECT CALENDAR_YEAR(CreatedDate), SUM(Amount)
2FROM Opportunity
3GROUP BY CALENDAR_YEAR(CreatedDate)Date functions are available in API version 18.0 and later.
This table lists all the date functions supported by SOQL.
Note the following when you use date functions:
- You can use a date function in a WHERE clause to filter your results even if your query doesn't include
a GROUP BY clause. The following
query returns data for 2009:
1swfobject.registerObject("clippy.codeblock-1", "9");SELECT CreatedDate, Amount 2FROM Opportunity 3WHERE CALENDAR_YEAR(CreatedDate) = 2009 - You can't compare the result of a date function with a date literal in a WHERE clause. The following
query doesn't work:
1SELECT CreatedDate, Amount 2FROM Opportunity 3WHERE CALENDAR_YEAR(CreatedDate) = THIS_YEAR - You can't use a date function in a SELECT clause unless you also include it in the GROUP BY clause. There is an exception if the field used
in the date function is a date field. You can use the date field instead
of the date function in the GROUP BY clause. This doesn't work for dateTime fields. The following query
doesn't work because CALENDAR_YEAR(CreatedDate) is not in a GROUP BY clause:
1SELECT CALENDAR_YEAR(CreatedDate), Amount 2FROM OpportunityThe following query works because the date field, CloseDate, is in the GROUP BY clause. This wouldn't work for a dateTime field, such as CreatedDate.
1swfobject.registerObject("clippy.codeblock-4", "9");SELECT CALENDAR_YEAR(CloseDate) 2FROM Opportunity 3GROUP BY CALENDAR_YEAR(CloseDate)