Full answer
sql
SELECT strftime('%w', '2026-12-04') AS weekday, strftime('%j', '2026-12-04') AS day_of_year, typeof(strftime('%w', '2026-12-04')) AS result_type;| weekday | day_of_year | result_type |
|---|---|---|
| 5 | 338 | text |
strftime always returns text, so compare with '5' or cast before arithmetic. Days of the week run from Sunday (0) to Saturday (6); for a Monday-based number, (CAST(strftime('%w', d) AS INTEGER) + 6) % 7 gives Monday as 0. The day of year counts from '001'. PostgreSQL uses extract(dow FROM d) and extract(doy FROM d), which return numbers (not run here). Recent SQLite versions also support '%u' for an ISO weekday from 1 (Monday) to 7.
sql
SELECT strftime('%w', '2026-12-04') AS weekday, strftime('%j', '2026-12-04') AS day_of_year, typeof(strftime('%w', '2026-12-04')) AS result_type;Checked on SQLite 3.46.1, run twice with identical output.
Key points
- %w: Sunday = 0
- %j: day of year from 001
- strftime returns text
- PostgreSQL uses extract(dow / doy)
Common mistakes
- Comparing strftime output with integers without casting
Follow-ups
- How would you group orders by weekday name?
- How would you compute the ISO week number?
Tips
- Remember Sunday is 0