SOQL date literals: the complete cheat sheet
Filtering by date is the single most common thing people do in SOQL — and the place where SQL habits
break first. SOQL has no GETDATE() or DATEADD(). Instead it ships with
date literals: unquoted keywords like TODAY, THIS_QUARTER, and
LAST_N_DAYS:30 that Salesforce expands into date ranges for you. This cheat sheet covers
every literal, with examples and the gotchas that break queries.
How date literals work
A date literal is written bare — no quotes — wherever a date value would go in a WHERE clause. Salesforce evaluates it at query time, in the time zone of the user running the query:
SELECT Id, Name, Amount, CloseDate FROM Opportunity WHERE CloseDate = THIS_QUARTER AND IsClosed = false
Range-style literals like THIS_MONTH match any value inside the range, so =
reads as "falls within". You can also compare with <, >,
<=, and >= — for example CloseDate > NEXT_WEEK means
"after the end of next week".
Fixed-range literals
| Literal | Matches |
|---|---|
YESTERDAY / TODAY / TOMORROW | The full 24 hours of that day. |
LAST_WEEK / THIS_WEEK / NEXT_WEEK | Seven full days; the week's start day depends on your locale. |
LAST_MONTH / THIS_MONTH / NEXT_MONTH | The full calendar month. |
LAST_90_DAYS / NEXT_90_DAYS | A rolling 90-day window ending (or starting) today. |
LAST_QUARTER / THIS_QUARTER / NEXT_QUARTER | The full calendar quarter — see the fiscal variants below. |
LAST_YEAR / THIS_YEAR / NEXT_YEAR | The full calendar year. |
Rolling-range literals (the :n family)
These take a number after a colon and build a window relative to now:
| Literal | Matches |
|---|---|
LAST_N_DAYS:n | From midnight n days ago up to the current moment — note this includes today. |
NEXT_N_DAYS:n | From midnight tomorrow through the next n days. |
LAST_N_WEEKS:n / NEXT_N_WEEKS:n | Whole calendar weeks before/after the current week. |
LAST_N_MONTHS:n / NEXT_N_MONTHS:n | Whole calendar months before/after the current month. |
LAST_N_QUARTERS:n / NEXT_N_QUARTERS:n | Whole calendar quarters before/after the current quarter. |
LAST_N_YEARS:n / NEXT_N_YEARS:n | Whole calendar years before/after the current year. |
N_DAYS_AGO:n | Exactly the single day n days ago (also available for weeks, months, quarters, and years: N_MONTHS_AGO:n, etc.). |
For example, cases opened in the last 30 days (including today):
SELECT Id, CaseNumber, Subject FROM Case WHERE CreatedDate = LAST_N_DAYS:30
Fiscal period literals
Plain THIS_QUARTER is the calendar quarter. If your company's fiscal year is
configured in Salesforce Setup, use the fiscal variants to match how your business actually reports:
LAST_FISCAL_QUARTER/THIS_FISCAL_QUARTER/NEXT_FISCAL_QUARTERLAST_FISCAL_YEAR/THIS_FISCAL_YEAR/NEXT_FISCAL_YEARLAST_N_FISCAL_QUARTERS:n/NEXT_N_FISCAL_QUARTERS:nLAST_N_FISCAL_YEARS:n/NEXT_N_FISCAL_YEARS:nN_FISCAL_QUARTERS_AGO:n/N_FISCAL_YEARS_AGO:n
For example, won revenue so far this fiscal year:
SELECT SUM(Amount) FROM Opportunity WHERE IsWon = true AND CloseDate = THIS_FISCAL_YEAR
Absolute dates and datetimes
Literal dates are also written without quotes. Date fields take YYYY-MM-DD:
SELECT Id FROM Opportunity WHERE CloseDate >= 2026-07-01
Datetime fields need a full ISO 8601 value with a time zone — a bare date here is an error:
SELECT Id FROM Case WHERE CreatedDate >= 2026-06-01T00:00:00Z AND CreatedDate < 2026-07-01T00:00:00Z
The half-open pattern above (>= the first of the month, < the first of
the next month) is the reliable way to ask "everything in June" against a datetime field.
The gotchas that break date queries
- Never quote a literal.
WHERE CloseDate = 'THIS_QUARTER'treats it as a string and fails — literals are keywords, not values. - Date vs. datetime fields differ.
CreatedDateis a datetime, so comparing it to a bare2026-06-09is an error. Date literals likeTODAYwork on both field types, which is one more reason to prefer them. LAST_N_DAYSincludes today. It runs from midnight n days ago up to the current second. If you want "the 7 days before today, excluding today", you need explicit bounds.- Weeks follow your locale.
THIS_WEEKmay start Sunday or Monday depending on the running user's locale settings. - Quarters are calendar unless you say FISCAL. If your fiscal year starts in February,
THIS_QUARTERandTHIS_FISCAL_QUARTERreturn different rows. - Results depend on the user's time zone. Literals are evaluated in the time zone of whoever runs the query — two users in different time zones can get different rows for
TODAY.
Putting it together
Opportunities closing this fiscal quarter, broken down by stage:
SELECT StageName, COUNT(Id), SUM(Amount) FROM Opportunity WHERE CloseDate = THIS_FISCAL_QUARTER AND IsClosed = false GROUP BY StageName
Grouped queries like this one are covered in depth in our guide to SOQL aggregate queries — and if you're translating habits from another database, start with SOQL vs SQL.