Request a demo

BigQuery · Datetime function

BigQuery DATETIME_SUB

DATETIME_SUB(datetime, INTERVAL n part) subtracts microseconds up to years from a DATETIME and returns a DATETIME. Use it for local-time windows such as "the last 2 hours in store time"; month steps clamp to month ends.

GoogleSQL for BigQuery · Checked against the official reference in November 2026

DATETIME_SUB moves a DATETIME back by MICROSECOND, MILLISECOND, SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER or YEAR units. A negative amount moves forward. Subtracting a month from March 31 returns February 28 (or 29), because the target month has no 31st.

The arithmetic happens on the calendar and clock, with no time zone. Subtracting INTERVAL 1 DAY from a local time on the morning after a DST change returns the same clock time the previous day, which is 23 or 25 real hours earlier. Use TIMESTAMP_SUB when you need an exact duration.

To measure the gap between two DATETIME values, use DATETIME_DIFF. Subtracting them with - returns an INTERVAL.

Syntax

DATETIME_SUB(datetime_expression, INTERVAL step_size step_unit)

Arguments and return value

ArgumentDescription
datetime_expressionThe DATETIME to shift back.
step_sizeAn INT64 number of units; negative values move forward.
step_unitMICROSECOND, MILLISECOND, SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER or YEAR.

Returns: DATETIME

Examples

Crossing midnight and month ends

SELECT
  DATETIME_SUB(DATETIME '2026-11-23 00:15:00', INTERVAL 30 MINUTE) AS before_midnight,
  DATETIME_SUB(DATETIME '2026-03-31 08:00:00', INTERVAL 1 MONTH) AS month_back,
  DATETIME_SUB(DATETIME '2026-11-23 18:45:00', INTERVAL 1 WEEK) AS week_back;
Result
before_midnightmonth_backweek_back
2026-11-22T23:45:002026-02-28T08:00:002026-11-16T18:45:00

Shift start from shift end

WITH shifts AS (
  SELECT 'night' AS shift, DATETIME '2026-11-23 06:00:00' AS ends_at, 8 AS hours UNION ALL
  SELECT 'day', DATETIME '2026-11-23 18:00:00', 12
)
SELECT shift, DATETIME_SUB(ends_at, INTERVAL hours HOUR) AS starts_at
FROM shifts
ORDER BY starts_at;
Result
shiftstarts_at
night2026-11-22T22:00:00
day2026-11-23T06:00:00

Local "last 2 hours" window

Both sides of the comparison are Madrid wall-clock DATETIME values.

WITH store_events AS (
  SELECT 'e1' AS event_id, DATETIME_SUB(CURRENT_DATETIME('Europe/Madrid'), INTERVAL 30 MINUTE) AS local_time UNION ALL
  SELECT 'e2', DATETIME_SUB(CURRENT_DATETIME('Europe/Madrid'), INTERVAL 3 HOUR)
)
SELECT COUNT(*) AS recent_events
FROM store_events
WHERE local_time >= DATETIME_SUB(CURRENT_DATETIME('Europe/Madrid'), INTERVAL 2 HOUR);
Result
recent_events
1

Common mistakes

  • Mixing zones in a comparison. A DATETIME column in local time compared with DATETIME_SUB(CURRENT_DATETIME(), ...) in UTC is off by the UTC offset; pass the same zone to CURRENT_DATETIME.
  • Using DATETIME_SUB on a TIMESTAMP column. It doesn't return a TIMESTAMP; use TIMESTAMP_SUB for instants.
  • Expecting dt1 - dt2 to give hours. Subtraction returns an INTERVAL; use DATETIME_DIFF(dt1, dt2, HOUR).

FAQ

How do I subtract hours from a datetime in BigQuery?

DATETIME_SUB(dt, INTERVAL 2 HOUR) or dt - INTERVAL 2 HOUR. Both return a DATETIME.

What does DATETIME_SUB do at the end of a month?

It clamps: DATETIME_SUB(DATETIME '2026-03-31 08:00:00', INTERVAL 1 MONTH) returns 2026-02-28T08:00:00.

Errors you may hit

More datetime functions

All BigQuery datetime functions · All functions · BigQuery error messages

Observability for every BigQuery table, in about 15 minutes

Masthead detects freshness, volume and schema anomalies, monitors pipeline errors and maps column-level lineage across all your projects, from logs and metadata only.