DATE_TRUNC
DATE_TRUNC(unit, value) rounds a date or timestamp down to the start of a specified unit — such as day, month, or year — commonly used to bucket time-series data.
Overview
DATE_TRUNC truncates a date, timestamp, or interval to the beginning of a specified precision: year, quarter, month, week, day, hour, minute, second, and more. It's the standard way to bucket raw event timestamps into daily, weekly, or monthly aggregates.
Copy code
SELECT
DATE_TRUNC('month', order_timestamp) AS order_month,
SUM(revenue) AS monthly_revenue
FROM orders
GROUP BY ALL
ORDER BY ALL;
DATE_TRUNC('week', ts) truncates to the most recent Monday (ISO week start), while DATE_TRUNC('day', ts) zeroes out the time-of-day portion, leaving midnight.
DATE_TRUNC vs EXTRACT
DATE_TRUNC returns a value of the same type (date/timestamp) rounded down — useful for grouping and joining on time buckets. EXTRACT instead pulls out a single numeric field (like the month number) without preserving the full date. Use DATE_TRUNC when you need to group by a time period and still display or compare it as a date; use EXTRACT when you only need the bare number.
DuckDB specifics
DuckDB implements date_trunc('part', value) matching Postgres semantics, and also accepts the alias datetrunc(). It supports the same argument order and part names ('year', 'quarter', 'month', 'week', 'day', 'hour', 'minute', 'second', 'milliseconds', 'microseconds', 'decade', 'century'), and works over DATE, TIMESTAMP, and TIMESTAMPTZ columns. Combined with DuckDB's GROUP BY ALL, monthly rollups become compact:
Copy code
SELECT DATE_TRUNC('day', event_time) AS day, COUNT(*) AS events
FROM events
GROUP BY ALL
ORDER BY ALL;
Related terms
EXTRACT(field FROM value) pulls a single numeric component — such as year, month, or hour — out of a date, time, or timestamp value.
Role-playing dimension →A role-playing dimension is a single physical dimension table, most often a date dimension, that is joined into a fact table multiple times to represent different roles, such as order date, ship date, and delivery date.
INTERVAL →INTERVAL is a SQL data type and literal syntax representing a span of time — such as '3 days' or '1 month' — used for date/timestamp arithmetic.
GENERATE_SERIES →GENERATE_SERIES(start, stop, step) produces a sequence of numeric, date, or timestamp values, commonly used to build calendars or fill gaps in time-series data.
timestamps →Timestamps are a fundamental data type in databases and programming languages that represent a specific point in time, typically including both the date and…
Dimension table →A dimension table stores the descriptive, mostly-textual attributes — like customer name, product category, or region — that you use to filter, group, and label the measures in a fact table.
FAQS
DATE_TRUNC('month', ts) returns a date/timestamp rounded down to the start of that month, preserving the date type for grouping or comparison. EXTRACT(MONTH FROM ts) returns just the numeric month (1-12) with no date context.
Use DATE_TRUNC('week', timestamp_column), which rounds each timestamp down to the start of its ISO week (Monday), then GROUP BY the truncated value to aggregate weekly totals.
