ISO Week Year Splits Calendar Years
ISO week numbering moves the year boundary to the Monday containing January 1, so late December days can belong to the next year.
Code
CREATE TABLE receipts (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
taken_at date NOT NULL
);
INSERT INTO receipts (taken_at) VALUES
(DATE '2025-12-28'),
(DATE '2025-12-29'), -- Monday of ISO week 2026-W01
(DATE '2025-12-31'),
(DATE '2026-01-01');
-- calendar month keeps Dec 29-31 in December; ISO year puts the whole
-- week in 2026
SELECT
taken_at,
to_char(taken_at, 'IYYY') AS iso_year,
to_char(taken_at, 'IW') AS iso_week,
to_char(taken_at, 'YYYY-MM') AS calendar_month
FROM receipts
ORDER BY taken_at;Under the hood — ISO Week Year Splits Calendar Years
The file runs SELECT / INSERT / CREATE statements, touching receipts.
Clause by clause, expect: an ORDER BY that fixes the output order; NOT NULL qualifiers on the required columns; a PRIMARY KEY that enforces row identity; a GENERATED ALWAYS/GENERATED BY clause that computes the column value.
Applying it
Run the statement against the receipts table.
Statements that matter
SELECT
taken_at,
to_char(taken_at, 'IYYY') AS iso_year,
to_char(taken_at, 'IW') AS iso_week,
to_char(taken_at, 'YYYY-MM') AS calendar_month
FROM receipts
ORDER BY taken_at;
INSERT INTO receipts (taken_at) VALUES
(DATE '2025-12-28'),
(DATE '2025-12-29'), -- Monday of ISO week 2026-W01
(DATE '2025-12-31'),
(DATE '2026-01-01');
CREATE TABLE receipts (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
taken_at date NOT NULL
);
Interactive Tools for This Topic
ISO Week CalculatorFind the ISO 8601 week number, ISO week-year, week start and end dates, and day of year for any calendar date, with strict date validation.Age CalculatorCalculate exact age in years, months, and days between a birth date and a reference date, plus total days and the next birthday countdown.Business Days CalculatorCount the weekdays between two dates, excluding weekends and any holidays you list, and see how many calendar and weekend days are involved.SQL DDL to TypeScript ConverterConvert SQL CREATE TABLE DDL statements into strict TypeScript interfaces with nullable, array, enum, JSONB, and Date or ISO string column mappings.
FAQ
- Which tables does the SQL touch?
- The references are `receipts`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.