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.
Date functions are available in API version 18.0 and later.
This table lists all the date functions supported by SOQL.
| Date Function | Description | Examples |
|---|---|---|
| CALENDAR_MONTH() | Returns a number representing the calendar month of a date field. |
|
| CALENDAR_QUARTER() | Returns a number representing the calendar quarter of a date field. |
|
| CALENDAR_YEAR() | Returns a number representing the calendar year of a date field. | 2009 |
| DAY_IN_MONTH() | Returns a number representing the day in the month of a date field. | 20 for February 20 |
| DAY_IN_WEEK() | Returns a number representing the day of the week for a date field. |
|
| DAY_IN_YEAR() | Returns a number representing the day in the year for a date field. | 32 for February 1 |
| DAY_ONLY() | Returns a date representing the day portion of a dateTime field. | 2009-09-22 for September 22, 2009 You can only use DAY_ONLY() with dateTime fields. |
| FISCAL_MONTH() | Returns a number representing the fiscal month of a date field. This differs from CALENDAR_MONTH() if your organization uses a fiscal year that does not match the Gregorian calendar. | If your fiscal year starts in March:
See “Setting the Fiscal Year” in the Salesforce online help. |
| FISCAL_QUARTER() | Returns a number representing the fiscal quarter of a date field. This differs from CALENDAR_QUARTER() if your organization uses a fiscal year that does not match the Gregorian calendar. | If your fiscal year starts in July:
|
| FISCAL_YEAR() | Returns a number representing the fiscal year of a date field. This differs from CALENDAR_YEAR() if your organization uses a fiscal year that does not match the Gregorian calendar. | 2009 |
| HOUR_IN_DAY() | Returns a number representing the hour in the day for a dateTime field. | 18 for a time
of 18:23:10 You can only use HOUR_IN_DAY() with dateTime fields. |
| WEEK_IN_MONTH() | Returns a number representing the week in the month for a date field. | 2 for April
10 The first week is from the first through the seventh day of the month. |
| WEEK_IN_YEAR() | Returns a number representing the week in the year for a date field. | 1 for January
3 The first week is from January 1 through January 7. |
Note the following when you use date functions:
SELECT CreatedDate, Amount FROM Opportunity WHERE CALENDAR_YEAR(CreatedDate) = 2009
SELECT CreatedDate, Amount FROM Opportunity WHERE CALENDAR_YEAR(CreatedDate) = THIS_YEAR
SELECT CALENDAR_YEAR(CreatedDate), Amount FROM Opportunity
The 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.
SELECT CALENDAR_YEAR(CloseDate) FROM Opportunity GROUP BY CALENDAR_YEAR(CloseDate)