SQL/Dates & text 9 min

Working with dates

Extract parts, do date arithmetic and roll data up by month.

strftimedatemonth

Dates are stored here as ISO text ('2023-06-01'). That format sorts chronologically as plain text, and strftime pulls out any part of it.

sql · editable
loading editor…

Try it: Show the day of the year (%j) instead of the weekday.

date() applies modifiers to shift a date, and julianday() turns two dates into a difference in days — the usual way to measure age, lag or time-to-event.

sql · editable
loading editor…

Reporting almost always means grouping by a truncated date. Truncate to the month with strftime, then aggregate.

sql · editable
loading editor…

Try it: Count only completed orders in the monthly totals.

Other engines spell this differently — DATE_TRUNC('month', col) and EXTRACT(YEAR FROM col) in Postgres, DATE_FORMAT in MySQL — but the idea is identical.