SOQL Date Functions

SOQL supports some date functions which can be applied on Date and Datetime fields value in the WHERE Clause before comparing.

Here are a few commonly used Date Functions:

Function Name Description Example
CALENDAR_MONTH To find the Integer value of the current month. 1 for January, 12 for December
SELECT Id, Name
FROM Contact
WHERE Calendar_MONTH(Birthdate) = 8
Patients born in August.
CALENDAR_YEAR To find the year of a date field.
SELECT Id
FROM Contact
WHERE Calendar_YEAR(Birthdate) > 1965
Patients born after 1965
DAY_IN_WEEK To find the day at which the date points. 1 for Monday, 7 for Sunday
SELECT Id
FROM Contact
WHERE DAY_IN_WEEK(Birthdate) = 7
Patients born on a Sunday

These functions can not be used in Select Clauses while returning field values, unless used in a special Group BY Clauses which we see later.

For instance, the following query throws a syntax error:

SELECT Id, CALENDAR_YEAR(BirthDate)
FROM Contact