How do I group SQLite date values by calendar month?
Quick answer
For dates stored as consistent ISO-formatted text such as 2025-05-03, use strftime('%Y-%m', sale_date) to produce a year-month key, then group by that expression. For example, SELECT strftime('%Y-%m', sale_date) AS month, SUM(amount) FROM sales GROUP BY strftime('%Y-%m', sale_date);. This assumes valid date strings that SQLite’s date functions recognize. [S1]
The question, the details, the next step.
A practical checklist
Confirm how
sale_dateis stored and inspect representative values.Use
strftime('%Y-%m', sale_date)when the values are supported by SQLite’s date functions.Group by the year-month expression, not just a month number.
Compare a sample monthly total with the rows and amounts included in it.
A few more useful answers
Why use `%Y-%m` instead of `%m`?
%Y-%m keeps the year with the month, so January 2024 and January 2025 remain separate groups. Using only %m would combine them under the same month number.
Does this query work for every ISO-8601 date string?
Not necessarily. SQLite’s date functions accept supported time-value formats, not every ISO-8601 variation. Check that your stored representation is recognized and produces the expected month key. [S1]
Sources
References used for this guide.
- [S1] SQLite date and time functions: supported values and strftime sqlite.org · Checked
