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.

SqlData Integrity

Code

snippet
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

FAQ

Which tables does the SQL touch?
The references are `receipts`.