Get the first and last day of the year in SQL
Yearly rollups need the year window. Both boundaries come from the year number alone.
The task
make_date builds a date from year, month, day integers, so the year of d gives both boundaries directly.
The snippet
Written for PostgreSQL. Versions for SQL Server and MySQL are under Variations.
SELECT
make_date(EXTRACT(YEAR FROM d)::int, 1, 1) AS year_start,
make_date(EXTRACT(YEAR FROM d)::int, 12, 31) AS year_end
FROM (SELECT DATE '2026-03-04' AS d) x;
Как это работает
EXTRACT(YEAR FROM d) is the only variable; months and days are constants, so Dec 31 is always correct and leap years never enter the picture. The cast to int is required because make_date takes integers, not numerics.
Variations
SQL Server
SQL ServerSELECT
DATEFROMPARTS(YEAR(d), 1, 1) AS year_start,
DATEFROMPARTS(YEAR(d), 12, 31) AS year_end
FROM (SELECT CAST('2026-03-04' AS DATE) AS d) x;
MySQL
MySQLSELECT
MAKEDATE(YEAR(d), 1) AS year_start,
DATE_SUB(MAKEDATE(YEAR(d) + 1, 1), INTERVAL 1 DAY) AS year_end
FROM (SELECT CURDATE() AS d) x;
Questions that come up
Do I even need year_end?
For range filters, prefer the half-open form d >= year_start AND d < next_year_start. On a timestamp column, d <= year_end compares against midnight at the start of Dec 31 and misses everything later that day; the half-open form cannot.
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.