Dates and timesPostgreSQL only

DATE_TRUNC

Truncates a date and time to the start of the year, month, week, day or hour.

Syntax

DATE_TRUNC('<unit>', <timestamp>)

Drops everything smaller than the given unit: 'month' turns any moment of a month into its first day at 00:00. Units: 'year', 'quarter', 'month', 'week', 'day', 'hour', 'minute'. Most often used to group data by month or day.

Data for the examples

orders

idcreated_atamount
12026-07-03 10:15:00120.50
22026-07-28 18:40:0080.25
32026-08-09 09:05:00199.99
42026-08-21 14:30:0045.00
52026-09-02 11:00:0064.75

These tables already exist in the sandbox — the examples below use them. Want to try this in your own database? Copy the SQL.

One moment — different units
SELECT DATE_TRUNC('month', TIMESTAMP '2026-09-13 15:47') AS month_start,
DATE_TRUNC('day', TIMESTAMP '2026-09-13 15:47') AS day_start,
DATE_TRUNC('hour', TIMESTAMP '2026-09-13 15:47') AS hour_start;
month_startday_starthour_start
2026-09-01 00:00:002026-09-13 00:00:002026-09-13 15:00:00
Each order truncated to the start of its month
SELECT id, created_at,
DATE_TRUNC('month', created_at) AS month
FROM orders;
idcreated_atmonth
12026-07-03 10:15:002026-07-01 00:00:00
22026-07-28 18:40:002026-07-01 00:00:00
32026-08-09 09:05:002026-08-01 00:00:00
42026-08-21 14:30:002026-08-01 00:00:00
52026-09-02 11:00:002026-09-01 00:00:00
Orders by month
SELECT DATE_TRUNC('month', created_at) AS month,
COUNT(*) AS order_count,
SUM(amount) AS revenue
FROM orders
GROUP BY month
ORDER BY month;
monthorder_countrevenue
2026-07-01 00:00:002200.75
2026-08-01 00:00:002244.99
2026-09-01 00:00:00164.75

Watch out

A week starts on Monday: DATE_TRUNC('week', …) for Sunday, September 13 returns Monday, September 7.

If you pass a date, the result is timestamp with time zone, and how it looks depends on the time zone. If you need a plain date, add ::date: DATE_TRUNC('month', DATE '2026-09-13')::date.

To group by month, use DATE_TRUNC, not EXTRACT(MONTH FROM …): otherwise September of different years merges into a single row.

Related functions

Learn more in the course