Get the first and last day of the quarter in SQL
Quarterly reporting buckets every date into three-month windows that start in January, April, July and October.
The task
Integer arithmetic maps month 1-12 onto quarter start months 1, 4, 7, 10; EOMONTH finishes the job.
The snippet
Written for SQL Server. Versions for PostgreSQL are under Variations.
SELECT
DATEFROMPARTS(YEAR(d), (MONTH(d) - 1) / 3 * 3 + 1, 1) AS quarter_start,
EOMONTH(d, 2 - (MONTH(d) - 1) % 3) AS quarter_end
FROM (SELECT CAST('2026-05-20' AS DATE) AS d) x;
它的運作方式
(MONTH(d) - 1) / 3 * 3 + 1 turns months 1-3 into 1, 4-6 into 4, and so on. For the end, EOMONTH(d, n) moves n months from d's month end; the offset 2 - (MONTH(d) - 1) % 3 lands on the quarter's final month, whose last day is the quarter end. Leap-year Feb 29 is handled by EOMONTH itself.
Variations
PostgreSQL
PostgreSQLSELECT
date_trunc('quarter', d)::date AS quarter_start,
(date_trunc('quarter', d) + interval '3 months - 1 day')::date AS quarter_end
FROM (SELECT DATE '2026-05-20' AS d) x;
Questions that come up
Can I group by quarter directly?
Yes: GROUP BY the quarter_start expression and every date in the quarter shares one key. On PostgreSQL, date_trunc('quarter', d) is the shortest form of that key.
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.