Map a calendar date to a fiscal year in SQL
Companies run fiscal years that start in July, April or October. Every report needs dates mapped into those years.
The task
One CASE decides whether the date falls in the first or second half of the fiscal year and labels it accordingly.
The snippet
Written for PostgreSQL. Versions for SQL Server are under Variations.
SELECT
d,
CASE WHEN EXTRACT(MONTH FROM d) >= 7
THEN EXTRACT(YEAR FROM d)::int + 1
ELSE EXTRACT(YEAR FROM d)::int
END AS fiscal_year
FROM (VALUES (DATE '2026-03-15'), (DATE '2026-08-01')) AS t(d);
So funktioniert es
With a fiscal year starting July 1 and labeled by its ending year, July-through-December dates belong to next year's number and the rest to the current one. Change the 7 to your fiscal start month and the label to your convention (starting year or ending year) and the same shape works for any mapping.
Variations
SQL Server
SQL ServerSELECT
d,
CASE WHEN MONTH(d) >= 7 THEN YEAR(d) + 1 ELSE YEAR(d) END AS fiscal_year
FROM (VALUES (CAST('2026-03-15' AS DATE)), (CAST('2026-08-01' AS DATE))) AS t(d);
Questions that come up
Is FY2026 the year that starts or ends in 2026?
Both conventions exist. Labelling by the ending year is common in the US (the US federal FY2026 runs October 2025 to September 2026); others label by the starting year or write both years, as in the UK's 2026-27 tax year. The query above labels by ending year. State the convention in your reports or the numbers will be a year off for part of your audience.
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.