Sub topic 7
Date literals and date filters
This chapter assumes you can write an inline query with a WHERE clause and a bind variable. It also assumes you know that Salesforce has both Date and Datetime field types.
Date and Datetime fields
SOQL compares dates differently depending on the field type.
- A Date field stores a day only.
Opportunity.CloseDateis one. - A Datetime field stores a day and a time.
CreatedDateandLastModifiedDateare Datetime fields.
Fixed values are written without quotes. A Date looks like 2026-03-15. A Datetime looks like 2026-03-15T09:30:00Z, where the trailing Z means UTC.
List<Opportunity> closing = [
SELECT Id, Name, CloseDate
FROM Opportunity
WHERE CloseDate >= 2026-01-01 AND CloseDate <= 2026-03-31
];
List<Account> recent = [
SELECT Id, Name
FROM Account
WHERE CreatedDate >= 2026-03-15T00:00:00Z
];If you put quotes around a date, as in '2026-01-01', the query fails because the value is a string and not a date.
Date literals
A date literal is a keyword that SOQL turns into a date range when the query runs. Literals are relative to the current date, so the query never needs editing.
| Literal | Meaning |
|---|---|
| `TODAY`, `YESTERDAY`, `TOMORROW` | A single day |
| `THIS_WEEK`, `LAST_WEEK`, `NEXT_WEEK` | A calendar week |
| `THIS_MONTH`, `LAST_MONTH`, `NEXT_MONTH` | A calendar month |
| `THIS_QUARTER`, `THIS_YEAR`, `LAST_YEAR` | A quarter or year |
| `LAST_N_DAYS:n`, `NEXT_N_DAYS:n` | A rolling window of `n` days |
| `LAST_90_DAYS`, `NEXT_90_DAYS` | Fixed rolling windows |
Use = with a literal to test whether a value falls inside its range.
List<Case> thisMonth = [
SELECT Id, Subject FROM Case WHERE CreatedDate = THIS_MONTH
];
List<Opportunity> overdue = [
SELECT Id, Name FROM Opportunity
WHERE CloseDate < TODAY AND IsClosed = false
];
List<Task> upcoming = [
SELECT Id, Subject FROM Task WHERE ActivityDate = NEXT_N_DAYS:14
];The n in LAST_N_DAYS:n is part of the literal. You cannot write LAST_N_DAYS: followed by a bind variable.
Computed dates with bind variables
When the window comes from Apex logic, calculate the value and bind it. Use Date for Date fields and Datetime for Datetime fields.
Integer days = 30;
Datetime cutoff = Datetime.now().addDays(-days);
Date cutoffDate = Date.today().addDays(-days);
List<Account> staleAccounts = [
SELECT Id, Name FROM Account WHERE LastModifiedDate < :cutoff
];
List<Opportunity> oldOpps = [
SELECT Id, Name FROM Opportunity WHERE CloseDate < :cutoffDate
];Common errors
- Quoting dates. Write dates without quotes.
- Mismatched types. Compare a Date field with a
Dateand a Datetime field with aDatetime. ADatebound to a Datetime field is read as midnight. - Time zones. Literals such as
TODAYuse the running user's time zone. Datetime values written withZare UTC. A query can return different rows for users in different zones. - Surprise boundaries.
THIS_WEEKstarts on the day set by the user's locale, which can be Sunday or Monday.
Exercise
Write a query that returns the Id and Name of every Opportunity that is still open and has a CloseDate in the next 30 days. Which literal do you use, and why not a bind variable?
Show answer
SELECT Id, Name FROM Opportunity WHERE IsClosed = false AND CloseDate = NEXTNDAYS:30. NEXTNDAYS:30 is a built-in literal, so the query needs no Apex date arithmetic. A bind variable would also work, for example CloseDate <= :Date.today().addDays(30), but a bind cannot be placed inside the literal itself.