Get the first and last day of the week in SQL
Weekly reports need every day of a week to share one key: the Monday it starts on.
The task
date_trunc('week', ...) snaps to Monday, and adding six days gives the Sunday. ISO weeks are Monday-based.
The snippet
Written for PostgreSQL. Versions for SQL Server and MySQL are under Variations.
SELECT
date_trunc('week', ordered_at)::date AS week_start,
(date_trunc('week', ordered_at) + interval '6 days')::date AS week_end
FROM orders;
How it works
date_trunc('week', ts) returns midnight of that week's Monday per the ISO definition. Casting to date drops the time, and adding six days yields the Sunday that closes the week. SQL Server's DATEPART(WEEKDAY) and MySQL's DAYOFWEEK number the days differently, and SQL Server's even depends on SET DATEFIRST, so pin the convention in the query rather than trusting session settings.
Variations
SQL Server (ISO Monday, DATEFIRST-independent)
SQL ServerSELECT
DATEADD(DAY, -(DATEPART(WEEKDAY, d) + @@DATEFIRST + 5) % 7, d) AS week_start,
DATEADD(DAY, 6 - (DATEPART(WEEKDAY, d) + @@DATEFIRST + 5) % 7, d) AS week_end
FROM (SELECT CAST('2026-03-04' AS DATE) AS d) x;
MySQL
MySQLSELECT
d - INTERVAL WEEKDAY(d) DAY AS week_start,
d + INTERVAL (6 - WEEKDAY(d)) DAY AS week_end
FROM (SELECT CURDATE() AS d) x;
Questions that come up
Why do SQL Server week formulas involve @@DATEFIRST?
DATEPART(WEEKDAY) is relative to the DATEFIRST setting (which day counts as day 1). The combination of DATEPART and @@DATEFIRST in one expression cancels that setting out, giving a stable ISO Monday regardless of server configuration.
Keep reading
Run these with autocomplete that knows your tables
SQLly is a native SQL workbench for macOS, Windows and Linux. Paste a snippet into a live connection and you get schema-aware IntelliSense, a plan viewer and guards against unsafe edits.