Extract the hour from a datetime or timestamp in BigQuery

How BigQuery's EXTRACT(HOUR ...) works on TIMESTAMP, DATETIME and TIME values, how time zones change the result, and when FORMAT or TRUNC functions are a better fit.

Pulling the hour out of a time value is one of the most common clock-time extractions in BigQuery. It is used for hourly traffic charts, shift analysis and peak-time reports. The function itself is short, but the result depends on the input data type and, for timestamps, on the time zone you ask for. This review covers what the extraction returns, where it can mislead, and which alternative to use when a bare hour number is not what you need.

Evidence note: no test results, sample datasets or measurements were supplied for this article. The behavior described here follows BigQuery's documented GoogleSQL semantics. The example outputs are worked from those rules, not captured from a live run. Confirm them in your own project, especially anything involving time zones.

Short answer: the core syntax

Use EXTRACT with the HOUR part. It accepts TIMESTAMP, DATETIME and TIME values and returns an INT64 from 0 to 23.

-- From a TIMESTAMP (interpreted in UTC unless you say otherwise)
SELECT EXTRACT(HOUR FROM event_ts) AS hour_utc
FROM my_dataset.events;

-- From a TIMESTAMP, in a specific time zone
SELECT EXTRACT(HOUR FROM event_ts AT TIME ZONE 'America/New_York') AS hour_ny
FROM my_dataset.events;

-- From a DATETIME (no time zone involved)
SELECT EXTRACT(HOUR FROM local_dt) AS hour_local
FROM my_dataset.bookings;

Is this extraction suitable for my intended use?

EXTRACT(HOUR ...) is a good fit when you want to compare activity by time of day and the hour number alone is the useful signal. Typical cases are "which hours are busiest" or filtering to business hours. The main benefit is a small integer that is cheap to group, filter and sort.

The key tradeoff is that the result discards everything except the hour: the date, the minutes and, for timestamps, the time zone context are all gone. Two events at 9 a.m. on different days, or 9 a.m. in different zones, become the same value 9. Whether that is acceptable depends on your question.

  • Use EXTRACT(HOUR ...) when you need an hour-of-day number for grouping, filtering or bucketing across many days.
  • Use TIMESTAMP_TRUNC or DATETIME_TRUNC with HOUR when you need a distinct bucket for each hour of each day, such as an hourly time series.
  • Use FORMAT_TIMESTAMP or FORMAT_DATETIME with %H when the output is a label for display or export and you want a zero-padded string such as 09.
  • Keep the original full value when the hour is only a convenience; extracted hours cannot be turned back into the source time.

What does the extraction actually change?

EXTRACT does not modify stored data. It reads a value and returns a new INT64 computed from it. The source column is untouched unless you write the result elsewhere. What it computes depends on the input type.

Input type | Time zone handling | HOUR result
TIMESTAMP | Represents an absolute instant; the hour is read in UTC by default, or in the zone given with AT TIME ZONE | INT64, 0 to 23, for that zone
DATETIME | Has no time zone; the stored wall-clock hour is returned as-is; AT TIME ZONE is not accepted | INT64, 0 to 23
TIME | Clock time only, no date or zone | INT64, 0 to 23
DATE | No time component | HOUR is not a valid part; the query errors
STRING | Not a time type | Must be parsed or cast first

The practical distinction is between a TIMESTAMP and a DATETIME. A TIMESTAMP stores a point in time, so its hour is a matter of which clock you read it on. A DATETIME stores a wall-clock reading with no zone attached, so its hour is fixed at whatever was stored. If the person who loaded the data recorded local time as DATETIME, BigQuery cannot know which zone that was.

Worked examples

SELECT
  EXTRACT(HOUR FROM TIMESTAMP '2026-01-15 14:30:00 UTC') AS utc_hour,                                   -- 14
  EXTRACT(HOUR FROM TIMESTAMP '2026-01-15 14:30:00 UTC' AT TIME ZONE 'America/New_York') AS ny_winter, -- 9  (UTC-5)
  EXTRACT(HOUR FROM TIMESTAMP '2026-07-15 14:30:00 UTC' AT TIME ZONE 'America/New_York') AS ny_summer, -- 10 (UTC-4)
  EXTRACT(HOUR FROM DATETIME '2026-01-15 14:30:00') AS dt_hour,                                         -- 14
  EXTRACT(HOUR FROM TIME '23:59:59') AS time_hour;                                                      -- 23

The two New York results show why a fixed UTC offset is a poor substitute for a named zone. Daylight saving time shifts the local hour for the same UTC clock reading. Use IANA zone names such as America/Chicago rather than hard-coded offsets when local time is what you mean.

Inputs that are not already a time type

Hour extraction only works on proper time types. Raw strings and Unix epoch numbers have to be converted first. SAFE.PARSE_TIMESTAMP and SAFE_CAST return NULL instead of failing on malformed rows. That is convenient, but it can hide bad data, so count the NULLs.

-- ISO-like string
SELECT EXTRACT(HOUR FROM SAFE_CAST('2026-01-15 14:30:00' AS TIMESTAMP)) AS h;

-- Custom string format
SELECT EXTRACT(HOUR FROM SAFE.PARSE_TIMESTAMP('%d/%m/%Y %H:%M', '15/01/2026 14:30')) AS h;

-- Unix epoch seconds or milliseconds
SELECT
  EXTRACT(HOUR FROM TIMESTAMP_SECONDS(epoch_s)) AS h_from_seconds,
  EXTRACT(HOUR FROM TIMESTAMP_MILLIS(epoch_ms)) AS h_from_millis
FROM my_dataset.raw_events;

A string without an explicit zone is interpreted as UTC when cast or parsed to TIMESTAMP. If the source strings are actually local times, either parse them as DATETIME, or pass the zone to PARSE_TIMESTAMP as its third argument.

Will the result remain readable and usable downstream?

For hour extraction, the usual concerns about copying text, screen readers and display order do not apply. The output is a plain integer or, with FORMAT functions, a plain ASCII string. The real usability risks come from how the value is interpreted once it leaves the query.

  • Integer versus string: EXTRACT returns 9, while FORMAT_TIMESTAMP('%H', ...) returns the STRING '09'. Strings sort correctly only while zero-padded; mixing the two types in joins or comparisons causes errors or mismatches.
  • Lost zone context: a column named hour with no zone in its name is easy to misread later. Naming it hour_utc or hour_local_ny documents the assumption.
  • Daylight saving gaps and repeats: on the spring-forward day, one local hour does not occur. On the fall-back day, one local hour occurs twice, so hourly counts for that day look uneven. This is correct behavior, not a bug, but it can surprise report readers.
  • Aggregation across days: grouping by the extracted hour merges all days together. If a chart is meant to show a timeline, truncate instead.
  • NULL handling: a NULL input produces a NULL hour, which most aggregations silently skip.

Alternatives side by side

SELECT
  EXTRACT(HOUR FROM event_ts)                                AS hour_int,     -- e.g. 9 (INT64)
  FORMAT_TIMESTAMP('%H', event_ts)                           AS hour_label,   -- e.g. '09' (STRING, UTC)
  FORMAT_TIMESTAMP('%H', event_ts, 'America/New_York')       AS hour_label_ny,
  TIMESTAMP_TRUNC(event_ts, HOUR)                            AS hour_bucket,  -- e.g. 2026-01-15 09:00:00 UTC
  TIMESTAMP_TRUNC(event_ts, HOUR, 'America/New_York')        AS hour_bucket_ny,
  DATETIME(event_ts, 'America/New_York')                     AS local_dt      -- full local wall-clock value
FROM my_dataset.events;

Converting a TIMESTAMP to a local DATETIME once, then extracting from it, is a readable pattern when a query needs several local parts such as hour, day of week and date. It keeps the zone decision in one place instead of repeating AT TIME ZONE in every expression.

A typical hourly breakdown

SELECT
  EXTRACT(HOUR FROM event_ts AT TIME ZONE 'America/Los_Angeles') AS hour_local_la,
  COUNT(*) AS events
FROM my_dataset.events
WHERE event_ts >= TIMESTAMP '2026-10-01 00:00:00 America/Los_Angeles'
  AND event_ts <  TIMESTAMP '2026-11-01 00:00:00 America/Los_Angeles'
GROUP BY hour_local_la
ORDER BY hour_local_la;

The date filter uses the same zone as the extraction, so the month boundaries line up with the local hours being counted. In this example, hours with no events will be missing rather than shown as zero. If the report needs all 24 rows, join against GENERATE_ARRAY(0, 23).

Gaps and what to verify

  • No sample data or query outputs were provided, so the example results above are derived from documented rules and should be checked in your environment.
  • Confirm whether your source column is TIMESTAMP, DATETIME or STRING before choosing a method; the schema decides which approach is valid.
  • If data was loaded from another system, confirm what zone its local times were recorded in; BigQuery cannot recover that from a DATETIME value.
  • Behavior in BI tools or exports that consume these columns was not tested here; check how they display integers versus zero-padded strings.