SQL reference

Format dates in SQL (month, year, weekday)

In Excel, you'd reach for
fx=TEXT(A2, "yyyy-mm"), =YEAR(), =MONTH(), =WEEKDAY()

Reports group by month, week or weekday, so the first job is usually turning a full date into the piece you need. In SQLite that's strftime(format, date), with codes like %Y (year), %m (month), %d (day), %W (week number) and %w (weekday, 0 = Sunday).

Syntax

-- SQLite (this site)
strftime('%Y-%m', d)
Try itEdit the query, then Run
order_dateyear_monthyearweek_numberweekday
2026-01-012026-01202600Thu
2026-01-012026-01202600Thu
2026-01-012026-01202600Thu
2026-01-022026-01202600Fri
2026-01-022026-01202600Fri
2026-01-022026-01202600Fri
2026-01-032026-01202600Sat
2026-01-042026-01202600Sun

8 rows

In other databases

  • Postgres, Snowflake, Oracle: TO_CHAR(d, 'YYYY-MM'), TO_CHAR(d, 'Dy'); to group, DATE_TRUNC('month', d)
  • MySQL: DATE_FORMAT(d, '%Y-%m'), DAYNAME(d)
  • SQL Server: FORMAT(d, 'yyyy-MM'), DATENAME(weekday, d)
  • BigQuery: FORMAT_DATE('%Y-%m', d); to group, DATE_TRUNC(d, MONTH)

For grouping, truncating (keeping a real date, the 1st of the month) beats formatting to text: it sorts and charts correctly.

Your turn

Which weekday gets the most orders? Show weekday_number (strftime '%w') and orders, most first.

This page on one card

Format dates in SQL (month, year, weekday): the Excel way (=TEXT(A2) and the SQL query, side by side
Format dates in SQL (month, year, weekday), on one card: the Excel way and the SQL. Download · Save to Pinterest
The practice data (7 tables)

See every column and sample rows