← Back to SQL, page 8

sql-0157 · Medium mid

What day-of-week numbers do SQLite and PostgreSQL return for Sunday 2026-03-01 and Monday 2026-03-02?

SQLite's strftime('%w') returns 0 for Sunday and 1 for Monday. PostgreSQL's EXTRACT(DOW) uses the same 0 = Sunday scheme, while EXTRACT(ISODOW) returns 1 for Monday and 7 for Sunday. Weekend filters must use the numbering of the function in use.

Full answer
sql
SELECT strftime('%w', '2026-03-01') AS sun_w, strftime('%w', '2026-03-02') AS mon_w,
       typeof(strftime('%w', '2026-03-01')) AS t;
sun_wmon_wt
01text
sql
SELECT EXTRACT(DOW FROM DATE '2026-03-01') AS sun_dow, EXTRACT(ISODOW FROM DATE '2026-03-01') AS sun_isodow,
       EXTRACT(ISODOW FROM DATE '2026-03-02') AS mon_isodow;
sun_dowsun_isodowmon_isodow
071

SQLite 3.46 also accepts %u (ISO, Monday = 1 to Sunday = 7), matching PostgreSQL's ISODOW:

sql
SELECT strftime('%u', '2026-03-01') AS sun_u, strftime('%u', '2026-03-02') AS mon_u;
sun_umon_u
71

A filter such as strftime('%w', d) IN ('0', '6') selects weekends in SQLite. Note that strftime returns text, so compare with '0' and '6', or cast the result. Many off-by-one bugs come from mixing Sunday-first and Monday-first numbering across tools.

sql
SELECT strftime('%w', '2026-03-01') AS sun_w, strftime('%w', '2026-03-02') AS mon_w,
       typeof(strftime('%w', '2026-03-01')) AS t;

SELECT strftime('%u', '2026-03-01') AS sun_u, strftime('%u', '2026-03-02') AS mon_u;

Checked on SQLite 3.46.1, run twice with identical output. PostgreSQL results in the answer were checked on PostgreSQL 17.12.

Key points

  • SQLite %w: 0 = Sunday
  • PostgreSQL DOW: 0 = Sunday; ISODOW: 7 = Sunday
  • SQLite %u gives ISO numbering
  • strftime returns text, not numbers

Common mistakes

  • Comparing strftime output to integers
  • Assuming Monday = 0 or 1 without checking

Follow-ups

  • How would you count orders per weekday?
  • How would you compute ISO week numbers?

Tips

  • Ask which week convention the business uses