← Back to SQL, page 25

sql-0485 · Medium mid

What do strftime('%w', '2026-12-04') and strftime('%j', '2026-12-04') return?

'%w' gives the day of the week with Sunday as 0, so Friday 4 December 2026 returns '5'. '%j' gives the day of the year, '338', because January to November 2026 contain 334 days. Both results are text.

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;
weekdayday_of_yearresult_type
5338text

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