SQL DATE_TRUNC
Rounds a date or timestamp DOWN to the start of a specified unit — the exact opposite job from EXTRACT, which pulls a piece OUT.
What & Why
DATE_TRUNC('unit', source) rounds a timestamp down to the beginning of whatever unit you specify — truncating to 'month' zeroes out the day and time, landing on the 1st of that month at midnight. This is genuinely different from EXTRACT: EXTRACT pulls one number out and discards everything else; DATE_TRUNC keeps a full date/timestamp value, just rounded down.
See How It Works
BUSINESS QUESTION
Marketing wants each lead timestamp rounded down to its year, month, and day boundaries.
| id | campaign_id | created_at | qualified_at | converted_at | lead_score | source | country | |
|---|---|---|---|---|---|---|---|---|
| 301 | 1 | ana@example.com | 2024-01-21 09:10:00+00 | 2024-01-22 11:00:00+00 | 2024-02-02 10:00:00+00 | 86 | google_ads | US |
| 302 | 1 | ben@example.com | 2024-01-24 12:40:00+00 | NULL | NULL | 52 | google_ads | CA |
| 303 | 2 | chloe@example.com | 2024-02-16 08:30:00+00 | 2024-02-18 14:20:00+00 | NULL | 74 | GB | |
| 304 | 3 | dev@example.com | 2024-03-20 17:15:00+00 | 2024-03-21 09:00:00+00 | 2024-04-04 16:00:00+00 | 91 | US |
Watch the same timestamps truncate at three levelsStep 1 of 3
DATE_TRUNC('year', created_at)::date
| id | created_at | signup_year |
|---|---|---|
| 301 | 2024-01-21 09:10:00+00 | 2024-01-01 |
| 302 | 2024-01-24 12:40:00+00 | 2024-01-01 |
| 303 | 2024-02-16 08:30:00+00 | 2024-01-01 |
| 304 | 2024-03-20 17:15:00+00 | 2024-01-01 |
Year truncation moves every displayed 2024 timestamp to the first day of the year.
EXAMPLE QUERY
SELECT
id,
created_at,
DATE_TRUNC('year', created_at)::date AS signup_year,
DATE_TRUNC('month', created_at)::date AS signup_month,
DATE_TRUNC('day', created_at)::date AS signup_day
FROM marketing.leads
ORDER BY created_at, id;Sign up free to try it on a real business scenario