Sub topic 10
Extracting Fields and Truncating with `date_trunc`
Assume you know PostgreSQL date and time types, timestamptz, and AT TIME ZONE. This chapter shows how to inspect parts of a temporal value and group timestamps into calendar units.
Extracting fields
extract returns one part of a date, time, timestamp, or interval as a numeric value. Common fields include year, month, day, hour, dow (day of week), and epoch (seconds from the Unix epoch).
SELECT extract(year FROM TIMESTAMP '2024-06-15 13:45:20') AS year,
extract(month FROM TIMESTAMP '2024-06-15 13:45:20') AS month,
extract(hour FROM TIMESTAMP '2024-06-15 13:45:20') AS hour,
extract(dow FROM TIMESTAMP '2024-06-15 13:45:20') AS day_of_week;The result of extract is numeric, even when the value represents a whole number. dow is zero for Sunday, one for Monday, and so on through six for Saturday. For ISO week-based reporting, use fields such as isodow and isoyear.
PostgreSQL also supports date_part, which is equivalent to extract:
SELECT date_part('month', TIMESTAMP '2024-06-15 13:45:20');Use extract when possible because its syntax makes the source value and requested field easier to see.
Truncating to a calendar unit
date_trunc removes the smaller fields from a temporal value. Truncating to day produces the beginning of that day. Truncating to month produces the first day of the month at midnight.
SELECT date_trunc('day', TIMESTAMPTZ '2024-06-15 13:45:20+00') AS day_start,
date_trunc('month', TIMESTAMPTZ '2024-06-15 13:45:20+00') AS month_start;For a timestamptz, the result is still an instant. PostgreSQL displays that instant using the session TimeZone, so the displayed clock time can change when the session zone changes.
| Operation | Meaning | Typical use |
|---|---|---|
| `extract('month' FROM value)` | Returns one numeric field | Filtering or labeling |
| `date_trunc('month', value)` | Returns the start of a month | Grouping or bucketing |
| `date_trunc('hour', value)` | Returns the start of an hour | Time-series reports |
Truncating in a local time zone
Suppose starts_at is a timestamptz, but a report must group events by New York calendar day. Convert to the local zone before truncating:
SELECT date_trunc(
'day',
starts_at AT TIME ZONE 'America/New_York'
) AS local_day,
count(*) AS event_count
FROM event
GROUP BY 1
ORDER BY 1;Here, AT TIME ZONE first produces a plain timestamp containing New York’s wall-clock time. date_trunc then finds that local day’s midnight.
On PostgreSQL 12 and later, you can use the time-zone argument directly:
SELECT date_trunc(
'day',
starts_at,
'America/New_York'
) AS day_start_instant,
count(*) AS event_count
FROM event
GROUP BY 1
ORDER BY 1;This form returns a timestamptz representing midnight in New York. It is useful when the bucket boundary must remain an instant.
Exercise
How would you group a timestamptz column named created_at by calendar day in Tokyo?
Show answer
Use date_trunc('day', created_at AT TIME ZONE 'Asia/Tokyo') and group by that expression, or use date_trunc('day', created_at, 'Asia/Tokyo') on PostgreSQL 12 and later.