DOW Versus ISODOW Numbering
EXTRACT(DOW) numbers Sunday as 0 through Saturday 6, while ISODOW follows ISO 8601 with Monday 1 through Sunday 7.
Code
-- EXTRACT(DOW):周日=0 ... 周六=6(美式编号)
-- EXTRACT(ISODOW):周一=1 ... 周日=7(ISO 8601 编号)
SELECT
d,
EXTRACT(DOW FROM d) AS dow_number,
EXTRACT(ISODOW FROM d) AS isodow_number,
TO_CHAR(d, 'Dy') AS day_name
FROM (VALUES
(DATE '2026-11-15'), -- Sunday
(DATE '2026-11-16'), -- Monday
(DATE '2026-11-21'), -- Saturday
(DATE '2026-11-22') -- Sunday
) AS days(d);
-- 周末过滤:用 ISODOW IN (6, 7) 不依赖“周日是 0”的隐式约定
SELECT COUNT(*) AS weekend_days
FROM (VALUES (DATE '2026-11-15'), (DATE '2026-11-16')) AS days(d)
WHERE EXTRACT(ISODOW FROM d) IN (6, 7);How the snippet works
The file runs SELECT statements, touching d.
What the statements actually assemble: a WHERE clause narrowing which rows one statement affects; COUNT aggregation.
Applying it
The d table is the working surface here.
Statements that matter
SELECT COUNT(*) AS weekend_days
FROM (VALUES (DATE '2026-11-15'), (DATE '2026-11-16')) AS days(d)
WHERE EXTRACT(ISODOW FROM d) IN (6, 7);
SELECT
d,
EXTRACT(DOW FROM d) AS dow_number,
EXTRACT(ISODOW FROM d) AS isodow_number,
TO_CHAR(d, 'Dy') AS day_name
FROM (VALUES
(DATE '2026-11-15'), -- Sunday
(DATE '2026-11-16'), -- Monday
(DATE '2026-11-21'), -- Saturday
(DATE '2026-11-22') -- Sunday
) AS days(d);
Interactive Tools for This Topic
PostgreSQL EXPLAIN AnalyzerAnalyze PostgreSQL EXPLAIN ANALYZE text or FORMAT JSON plans locally to flag Seq Scans, slow nodes, row misestimates, disk sorts, and index hints.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.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.
FAQ
- Which tables does the SQL touch?
- The references are `d`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.