SQL Date Functions Cheat Sheet
The same five questions in five dialects: truncate, add, diff, extract and build a spine. Then convert time zones before you group by day.

What the sheet covers
Date and time functions side by side for DuckDB, PostgreSQL, Snowflake, BigQuery and SQL Server: truncate, week start, add or subtract, date difference, extract, last day of month, and a date spine. Store UTC as a timestamp without a time zone; the double AT TIME ZONE form is for that type. Convert once, then truncate to the local day. SQL Server uses Windows zone names; the others use IANA names such as America/New_York. Use half-open ranges (ts >= start AND ts < end). BETWEEN with a date as the end misses everything after midnight on the last day. Seconds-between functions are not interchangeable: some count second boundaries, PostgreSQL EXTRACT(EPOCH) is elapsed seconds, and BigQuery TIMESTAMP_DIFF is whole elapsed seconds. A generated day spine (generate_series with INTERVAL '1 day' in DuckDB and PostgreSQL) left-joined to facts keeps days with no orders as zero instead of dropping them.
Topics on the sheet
- Truncate, add and find the last day
- Week start, diffs and extract
- Fill missing days with a spine
- Time zones
- Gotchas
Download the PDF
Date and time functions as a one-page PDF in Letter and A4, plus all six sheets in one SQL cheat sheet PDF. Free PDF, download it right after you sign up. Signing up joins the dataengineers.club newsletter and unlocks the PDF bundle in Letter and A4. Unsubscribe anytime.
Truncate, add and find the last day
DuckDB and PostgreSQL use date_trunc and INTERVAL. Snowflake: DATE_TRUNC / DATEADD. BigQuery: DATE_TRUNC / DATE_SUB. SQL Server: DATETRUNC (2022) or DATEFROMPARTS; DATEADD; EOMONTH.
- Truncate to month: date_trunc('month', ts) (DuckDB, PostgreSQL). Snowflake DATE_TRUNC('MONTH', ts). BigQuery DATE_TRUNC(d, MONTH) or TIMESTAMP_TRUNC(ts, MONTH). SQL Server DATETRUNC(month, ts) from 2022; older: DATEFROMPARTS(YEAR(d), MONTH(d), 1).
- Add or subtract: ts - INTERVAL '7 days' (DuckDB, PostgreSQL). d - 7: 7 must be INTEGER, not BIGINT in DuckDB. DATEADD(day, -7, d) on Snowflake and SQL Server (SQL Server has no INTERVAL type). BigQuery DATE_SUB(d, INTERVAL 7 DAY).
- Last day of month: last_day(d) / LAST_DAY(d) / EOMONTH(d). PostgreSQL: (date_trunc('month', d) + INTERVAL '1 month')::date - 1.
Week start, diffs and extract
- Week start is Monday (ISO) in DuckDB and PostgreSQL date_trunc('week', d). BigQuery DATE_TRUNC(d, WEEK) is Sunday; use WEEK(MONDAY) or ISOWEEK. Snowflake depends on WEEK_START. SQL Server DATETRUNC(week, d) depends on @@DATEFIRST (Sunday for us_english).
- Days between: b - a, or date_diff / DATEDIFF / DATE_DIFF. Argument order differs: DuckDB date_diff('day', a, b); BigQuery DATE_DIFF(b, a, DAY).
- Seconds between: date_diff('second', a, b) (DuckDB); EXTRACT(EPOCH FROM b - a) (PostgreSQL); DATEDIFF('second', a, b) (Snowflake); TIMESTAMP_DIFF(b, a, SECOND) (BigQuery); DATEDIFF(second, a, b) (SQL Server).
- Extract a part: EXTRACT(year FROM ts) or YEAR / DATEPART.
Fill missing days with a spine
Date spine: generate_series(d1, d2, INTERVAL '1 day') in DuckDB and PostgreSQL (gives timestamps: cast to DATE). Snowflake: TABLE(GENERATOR(ROWCOUNT => n)) with ROW_NUMBER and DATEADD. BigQuery: UNNEST(GENERATE_DATE_ARRAY(d1, d2)). SQL Server: recursive CTE or calendar table; SQL Server 2022 GENERATE_SERIES (compat 160+) returns numbers: add them to a start date with DATEADD.
WITH days AS (
SELECT CAST(d AS DATE) AS day
FROM generate_series(
DATE '2025-01-01',
DATE '2025-01-07',
INTERVAL '1 day') t(d)
)
SELECT days.day,
COALESCE(SUM(o.amount), 0) AS revenue
FROM days
LEFT JOIN orders o
ON CAST(o.ts AS DATE) = days.day
GROUP BY days.day
ORDER BY days.day;Days with no orders show 0 instead of disappearing. DuckDB and PostgreSQL syntax.
Time zones
- Store UTC as a timestamp without a time zone. The double AT TIME ZONE form is for that type. Convert once, then truncate to the local day: date_trunc('day', local_ts).
- A local day can last 23 or 25 hours when daylight saving time changes.
- SQL Server uses Windows zone names ('Eastern Standard Time'). The others use IANA names ('America/New_York').
- DuckDB and PostgreSQL: (ts AT TIME ZONE 'UTC') AT TIME ZONE 'America/New_York'. Snowflake: CONVERT_TIMEZONE('UTC', 'America/New_York', ts). BigQuery: DATETIME(ts, 'America/New_York') (ts is a TIMESTAMP).
Gotchas
WHERE ts >= DATE '2025-01-01'
AND ts < DATE '2025-02-01'- Use half-open ranges. BETWEEN with a date as the end misses everything after midnight on the last day.
- ts = DATE '2025-01-01' only matches midnight. Compare CAST(ts AS DATE). SQL Server: CAST('2025-01-01' AS date).
- DATEDIFF counts boundaries crossed: from Dec 31 to Jan 1 is 1 year in SQL Server and in DuckDB date_diff. DuckDB date_sub counts whole years: 0. Seconds between is not the same everywhere: DuckDB date_diff and Snowflake or SQL Server DATEDIFF count second boundaries; PostgreSQL EXTRACT(EPOCH FROM b - a) is elapsed seconds (can be fractional); BigQuery TIMESTAMP_DIFF is whole elapsed seconds.
- Day-of-week numbers differ: dow in PostgreSQL and DuckDB has 0 = Sunday, BigQuery DAYOFWEEK has 1 = Sunday. ISO isodow: 1 = Monday.
Engines named on the sheet
DuckDB, PostgreSQL, Snowflake, BigQuery, SQL Server. Side-by-side notes for DuckDB, PostgreSQL, Snowflake, BigQuery and SQL Server only. DuckDB 2.0.1 is the runtime used in the book. Do not read this list as a claim that every engine was executed for every example.
FAQ
How do I truncate a timestamp to the month?
date_trunc('month', ts) in DuckDB and PostgreSQL, DATE_TRUNC('MONTH', ts) in Snowflake, DATE_TRUNC(d, MONTH) or TIMESTAMP_TRUNC(ts, MONTH) in BigQuery, and DATETRUNC(month, ts) in SQL Server 2022 (older SQL Server: DATEFROMPARTS(YEAR(d), MONTH(d), 1)).
Which day does the week start on?
Monday (ISO) for date_trunc('week', d) in DuckDB and PostgreSQL. BigQuery DATE_TRUNC(d, WEEK) starts on Sunday; use WEEK(MONDAY) or ISOWEEK. Snowflake depends on WEEK_START and SQL Server on @@DATEFIRST.
Why does BETWEEN miss rows on the last day?
BETWEEN with a date as the end misses everything after midnight on that day. Use a half-open range: ts >= start AND ts < end.
How do I show days with no orders as zero?
Generate a date spine and left-join the facts to it, with COALESCE(SUM(amount), 0). In DuckDB and PostgreSQL, use generate_series(d1, d2, INTERVAL '1 day') and cast to DATE. SQL Server 2022 GENERATE_SERIES needs compatibility level 160+. Days with no orders then show 0 instead of disappearing.
Are the seconds-between functions interchangeable?
No. DuckDB date_diff and Snowflake or SQL Server DATEDIFF count second boundaries, PostgreSQL EXTRACT(EPOCH FROM b - a) is elapsed seconds (can be fractional), and BigQuery TIMESTAMP_DIFF is whole elapsed seconds.