BigQuery · Timestamp function
BigQuery TIMESTAMP_SUB
TIMESTAMP_SUB(ts, INTERVAL n part) subtracts an exact duration (MICROSECOND up to DAY) from a TIMESTAMP. TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 24 HOUR) is the standard "last 24 hours" filter; MONTH and YEAR are not allowed.
TIMESTAMP_SUB moves an instant back by MICROSECOND, MILLISECOND, SECOND, MINUTE, HOUR or DAY units, where a DAY is exactly 24 hours. A negative amount moves forward. It is the usual building block of rolling windows: WHERE event_ts >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 7 DAY).
Calendar units fail: TIMESTAMP_SUB(ts, INTERVAL 1 YEAR) returns "TIMESTAMP_SUB does not support the YEAR date part when the argument is TIMESTAMP type". For "the same time last month", convert to DATETIME in a zone, use DATETIME_SUB, and convert back.
A rolling 24-hour window is not the same as "yesterday". For complete calendar days in a zone, build the bounds with TIMESTAMP(DATE, zone) or truncate with TIMESTAMP_TRUNC.
Syntax
TIMESTAMP_SUB(timestamp_expression, INTERVAL step_size step_unit)Arguments and return value
| Argument | Description |
|---|---|
timestamp_expression | The TIMESTAMP to shift back. |
step_size | An INT64 amount; negative values move forward. |
step_unit | MICROSECOND, MILLISECOND, SECOND, MINUTE, HOUR or DAY (24 hours). |
Returns: TIMESTAMP
Examples
Subtract minutes, hours and days
SELECT
TIMESTAMP_SUB(TIMESTAMP '2026-11-23 00:10:00+00', INTERVAL 15 MINUTE) AS minus_15_min,
TIMESTAMP_SUB(TIMESTAMP '2026-11-23 00:10:00+00', INTERVAL 24 HOUR) AS minus_24_hours,
TIMESTAMP_SUB(TIMESTAMP '2026-11-23 00:10:00+00', INTERVAL 7 DAY) AS minus_7_days;| minus_15_min | minus_24_hours | minus_7_days |
|---|---|---|
| 2026-11-22 23:55:00 UTC | 2026-11-22 00:10:00 UTC | 2026-11-16 00:10:00 UTC |
Rolling 24-hour window
WITH errors AS (
SELECT 'job_a' AS job, TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 1 HOUR) AS failed_at UNION ALL
SELECT 'job_a', TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 23 HOUR) UNION ALL
SELECT 'job_b', TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 2 DAY)
)
SELECT job, COUNT(*) AS failures_24h
FROM errors
WHERE failed_at >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 24 HOUR)
GROUP BY job;| job | failures_24h |
|---|---|
| job_a | 2 |
Yesterday as complete local day bounds
In live queries use CURRENT_DATE('Europe/London') - 1 and CURRENT_DATE('Europe/London'). London is on UTC in November, so the bounds fall on UTC midnight; in summer they would be 23:00 UTC.
SELECT
TIMESTAMP(DATE '2026-11-22', 'Europe/London') AS yesterday_start,
TIMESTAMP(DATE '2026-11-23', 'Europe/London') AS yesterday_end_exclusive;| yesterday_start | yesterday_end_exclusive |
|---|---|
| 2026-11-22 00:00:00 UTC | 2026-11-23 00:00:00 UTC |
Common mistakes
- Using YEAR, QUARTER, MONTH or WEEK. They fail on TIMESTAMP; use DAY multiples or DATETIME_SUB.
- Treating
TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 1 DAY)as yesterday. It is the last 24 hours, which starts in the middle of yesterday. - Writing
ts - INTERVAL '1 day'orDATEADD(day, -1, ts). UseTIMESTAMP_SUB(ts, INTERVAL 1 DAY)orts - INTERVAL 1 DAY. - Moving the arithmetic onto the partition column.
WHERE TIMESTAMP_ADD(ts, INTERVAL 1 DAY) > CURRENT_TIMESTAMP()works logically but reads worse thanWHERE ts > TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 1 DAY); keep the column bare.
Cost and performance
- TIMESTAMP_ADD, TIMESTAMP_SUB, TIMESTAMP_DIFF and TIMESTAMP_TRUNC are on BigQuery's list of functions that can wrap a partitioning column and still allow partition pruning when their other arguments are constants. Arithmetic with operators (
ts + INTERVAL 1 DAY > ...) is not; move the arithmetic to the constant side:ts > TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 1 DAY).
FAQ
How do I filter the last 24 hours in BigQuery?
WHERE ts >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 24 HOUR).
How do I subtract a year from a timestamp?
Go through DATETIME: TIMESTAMP(DATETIME_SUB(DATETIME(ts), INTERVAL 1 YEAR)). TIMESTAMP_SUB doesn't accept YEAR.
Can TIMESTAMP_SUB take a column as the amount?
Yes. TIMESTAMP_SUB(finished_at, INTERVAL duration_seconds SECOND) works as long as the amount is INT64.
Related
Errors you may hit
More timestamp functions
All BigQuery timestamp functions · All functions · BigQuery error messages
