soql date literals

SOQL Date Literals and Date Functions

When there is a need to compare a custom or standard Date or Datetime field on an Objects in Salesforce, it is often difficult to compare a date with an exact date or datetime value. It is not only time consuming for user but also cumbersome to note the exact date value being compared.

Salesforce Object Query Language overcomes this by offering Date Literal expressions that allows a user to compare date or datetime fields to fixed expressions.

Let’s dive deep into how some of these can prove to be highly valuable when querying by date filters.

Date Literals

Definition: A date literal is a fixed expression that represents a relative range of time, such as last month, this week, or next year.

Day

If you wish to compare a date or datetime field to a particular day or range of days then use one of the following literal expressions:

Date Literal Range Example
YESTERDAY Starts 00:00:00 the day before and continues for 24 hours. SELECT Id FROM Account WHERE CreatedDate = YESTERDAY
TODAY Starts 00:00:00 of the current day and continues for 24 hours. SELECT Id FROM Account WHERE CreatedDate > TODAY
TOMORROW Starts 00:00:00 after the current day and continues for 24 hours. SELECT Id FROM Opportunity WHERE CloseDate = TOMORROW
LAST_90_DAYS Starts with the current day and continues for the past 90 days. This includes the current day, not just previous days. So it includes 91 days in total. SELECT Id FROM Account WHERE CreatedDate = LAST_90_DAYS
NEXT_90_DAYS Starts 00:00:00 of the next day and continues for the next 90 days. SELECT Id FROM Opportunity WHERE CloseDate > NEXT_90_DAYS
LAST_N_DAYS:n For the number n provided, starts with the current day and continues for the past n days. This includes the current day, not just previous days. For example, LAST_N_DAYS:1 includes yesterday and today. SELECT Id FROM Account WHERE CreatedDate = LAST_N_DAYS:365
NEXT_N_DAYS:n For the number n provided, starts 00:00:00 of the next day and continues for the next n days. This does not include the current day. For example, NEXT_N_DAYS:1 is equivalent to TOMORROW. SELECT Id FROM Opportunity WHERE CloseDate > NEXT_N_DAYS:15


Week

If you wish to compare a date or datetime field to a particular week or range of weeks then use one of the following literal expressions:

Date Literal Range Example
LAST_WEEK Starts 00:00:00 on the first day of the week before the most recent first day of the week and continues for seven full days. Your locale determines the first day of the week. SELECT Id FROM Account WHERE CreatedDate > LAST_WEEK
THIS_WEEK Starts 00:00:00 on the most recent first day of the week on or before the current day and continues for seven full days. Your locale determines the first day of the week. SELECT Id FROM Account WHERE CreatedDate < THIS_WEEK
NEXT_WEEK Starts 00:00:00 on the most recent first day of the week after the current day and continues for seven full days. Your locale determines the first day of the week. SELECT Id FROM Opportunity WHERE CloseDate = NEXT_WEEK
NEXT_N_WEEKS:n For the number n provided, starts 00:00:00 of the first day of the next week and continues for the next n weeks. SELECT Id FROM Opportunity WHERE CloseDate > NEXT_N_WEEKS:4
LAST_N_WEEKS:n For the number n provided, starts 00:00:00 of the last day of the previous week and continues for the past n weeks. SELECT Id FROM Account WHERE CreatedDate = LAST_N_WEEKS:52


Month

If you wish to compare a date or datetime field to a particular month or range of months then use one of the following literal expressions:

Date Literal Range Example
LAST_MONTH Starts 00:00:00 on the first day of the month before the current day and continues for all the days of that month. SELECT Id FROM Opportunity WHERE CloseDate > LAST_MONTH
THIS_MONTH Starts 00:00:00 on the first day of the month that the current day is in and continues for all the days of that month. SELECT Id FROM Account WHERE CreatedDate < THIS_MONTH
NEXT_MONTH Starts 00:00:00 on the first day of the month after the month that the current day is in and continues for all the days of that month. SELECT Id FROM Opportunity WHERE CloseDate = NEXT_MONTH
NEXT_N_MONTHS:n For the number n provided, starts 00:00:00 of the first day of the next month and continues for the next n months. SELECT Id FROM Opportunity WHERE CloseDate > NEXT_N_MONTHS:2
LAST_N_MONTHS:n For the number n provided, starts 00:00:00 of the last day of the previous month and continues for the past n months. SELECT Id FROM Account WHERE CreatedDate = LAST_N_MONTHS:12


Quarter

If you wish to compare a date or datetime field to a particular quarter or range of quarters then use one of the following literal expressions:

Date Literal Range Example
THIS_QUARTER Starts 00:00:00 of the current quarter and continues to the end of the current quarter. SELECT Id FROM Account WHERE CreatedDate = THIS_QUARTER
LAST_QUARTER Starts 00:00:00 of the previous quarter and continues to the end of that quarter. SELECT Id FROM Account WHERE CreatedDate > LAST_QUARTER
NEXT_QUARTER Starts 00:00:00 of the next quarter and continues to the end of that quarter. SELECT Id FROM Account WHERE CreatedDate < NEXT_QUARTER
NEXT_N_QUARTERS:n Starts 00:00:00 of the next quarter and continues to the end of the nth quarter. SELECT Id FROM Account WHERE CreatedDate < NEXT_N_QUARTERS:2
LAST_N_QUARTERS:n Starts 00:00:00 of the previous quarter and continues to the end of the previous nth quarter. SELECT Id FROM Account WHERE CreatedDate > LAST_N_QUARTERS:2

Year

If you wish to compare a date or datetime field to a particular quarter or range of quarters then use one of the following literal expressions:

Date Literal Range Example
THIS_YEAR Starts 00:00:00 on January 1 of the current year and continues through the end of December 31 of the current year. SELECT Id FROM Opportunity WHERE CloseDate = THIS_YEAR
LAST_YEAR Starts 00:00:00 on January 1 of the previous year and continues through the end of December 31 of that year. SELECT Id FROM Opportunity WHERE CloseDate > LAST_YEAR
NEXT_YEAR Starts 00:00:00 on January 1 of the following year and continues through the end of December 31 of that year. SELECT Id FROM Opportunity WHERE CloseDate < NEXT_YEAR
NEXT_N_YEARS:n Starts 00:00:00 on January 1 of the following year and continues through the end of December 31 of the nth year. SELECT Id FROM Opportunity WHERE CloseDate < NEXT_N_YEARS:5
LAST_N_YEARS:n Starts 00:00:00 on January 1 of the previous year and continues through the end of December 31 of the previous nth year. SELECT Id FROM Opportunity WHERE CloseDate > LAST_N_YEARS:5

Date Functions

Date Functions become an added benefit when using date literals to compare in a SOQL query. Some of our favourite and most useful Date Functions are below:

Date Function Description Examples
CALENDAR_MONTH() Returns a number representing the calendar month of a date field. 1 for January;12 for December
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. 1 for Sunday;7 for Saturday
DAY_ONLY() Returns a date representing the day portion of a dateTime field. 2009-09-22 for September 22, 2009
HOUR_IN_DAY() Returns a number representing the hour in the day for a dateTime field. You can only use HOUR_IN_DAY() with dateTime fields. 18 for a time of 18:23:10
WEEK_IN_MONTH() Returns a number representing the week in the month for a date field. 2 for April 10

Tips & Tricks

Some tips and tricks that will be useful when trying to query accurate data based on date and datetime fields.

  • DateTime field values are stored as Coordinated Universal Time (UTC). SOQL queries return DateTime field values as UTC values. If you want to process these values in different time zones, your application might need to handle the conversion.
    • The simplest way to convert is to use the following expression: convertTimezone(datetimeField)
    • Note that you can only use convertTimezone() in a date function. The following query doesn’t work because there is no date function.
    • SELECT HOUR_IN_DAY(convertTimezone(CreatedDate)), SUM(Amount)
      FROM Opportunity
      GROUP BY HOUR_IN_DAY(convertTimezone(CreatedDate))
  • To filter a SOQL query on a datetime field with a day value, use the DAY_ONLY SOQL operator.
    • SELECT Id 
      FROM Account 
      WHERE DAY_ONLY(CreatedDate) = TODAY

CONCLUSION

Ideally one should use the above date literals and functions to ensure accurate data is retrieved and the cumbersome process of adding exact dates can be removed. However, if it is necessary to use exact date or datetime values, then the format of these should be specific to what is accepted by Salesforce.