SQL how-to
How to get the last day of the month in SQL
Updated · Tested on PostgreSQL 18.6 and SQLite 3.54.1 (results generated , re-checked on every release)
Short answer
PostgreSQL: (date_trunc('month', d) + INTERVAL '1 month - 1 day')::date. SQLite: date(d, 'start of month', '+1 month', '-1 day'). The first day is date_trunc('month', d)::date or date(d, 'start of month').
The task: For any date, find the first and the last day of its month.
First and last day of each hire month
PostgreSQL
SELECT name, hired_on,
date_trunc('month', hired_on)::date AS month_start,
(date_trunc('month', hired_on) + INTERVAL '1 month - 1 day')::date AS month_end
FROM staff
ORDER BY hired_on;SQLite
SELECT name, hired_on,
date(hired_on, 'start of month') AS month_start,
date(hired_on, 'start of month', '+1 month', '-1 day') AS month_end
FROM staff
ORDER BY hired_on;Result (PostgreSQL and SQLite, identical): 8 rows
| name | hired_on | month_start | month_end |
|---|---|---|---|
| Rosa Vega | 2024-01-10 | 2024-01-01 | 2024-01-31 |
| Omar Haddad | 2024-03-01 | 2024-03-01 | 2024-03-31 |
| Ben Carter | 2024-04-20 | 2024-04-01 | 2024-04-30 |
| Lia Moreau | 2024-06-15 | 2024-06-01 | 2024-06-30 |
| Ivan Petrov | 2024-08-01 | 2024-08-01 | 2024-08-31 |
| Yuki Tanaka | 2025-02-01 | 2025-02-01 | 2025-02-28 |
| Zara Ali | 2025-05-12 | 2025-05-01 | 2025-05-31 |
| Noah Kim | 2025-09-30 | 2025-09-01 | 2025-09-30 |
Try it: PostgreSQL playground · SQLite playground
How it works
- 1
date_trunc('month', d)PostgreSQL: back to midnight on the 1st of the month.
- 2
+ INTERVAL '1 month - 1 day'Forward to the 1st of the next month, then back one day.
- 3
date(d, 'start of month', '+1 month', '-1 day')SQLite: the same three moves as modifiers, applied left to right.
More examples
How many days a month has
The day number of the last day. February 2024 was a leap month.
PostgreSQL
SELECT d AS any_day,
EXTRACT(DAY FROM date_trunc('month', d) + INTERVAL '1 month - 1 day')::int AS days_in_month
FROM (VALUES (DATE '2024-02-10'), (DATE '2026-02-10'), (DATE '2026-04-30')) AS t(d);SQLite
SELECT d AS any_day,
CAST(strftime('%d', d, 'start of month', '+1 month', '-1 day') AS INTEGER) AS days_in_month
FROM (SELECT '2024-02-10' AS d UNION ALL SELECT '2026-02-10' UNION ALL SELECT '2026-04-30');Result (PostgreSQL and SQLite, identical): 3 rows
| any_day | days_in_month |
|---|---|
| 2024-02-10 | 29 |
| 2026-02-10 | 28 |
| 2026-04-30 | 30 |
Try it: PostgreSQL playground · SQLite playground
Variations
Everything in the current month
Filter with a half-open range so an index can be used: d >= date_trunc('month', CURRENT_DATE) AND d < date_trunc('month', CURRENT_DATE) + INTERVAL '1 month' (SQLite: d >= date('now', 'start of month') AND d < date('now', 'start of month', '+1 month')).
Common mistakes
Adding 30 days to the 1st: lands on the wrong day in most months.
Comparing
d <= last_dayagainst timestamps: anything after midnight on the last day is missed. Use< first day of next month.Writing
'+1 month'before'start of month'in SQLite: modifiers apply in order, so the 31st can overflow into the month after.
PostgreSQL and SQLite
PostgreSQL uses date_trunc and intervals; SQLite uses date() modifiers. Both pairs above returned the same dates.
Official documentation
Go deeper
- Guide: SQL date functions in PostgreSQL and SQLite
- Practice: SQL date practice: 7 exercises in PostgreSQL and SQLite
- Lesson: Date maths (Pro)
Make it stick in five minutes a day
“Date maths” teaches this step by step on a real database, with instant feedback. Part of sqlstreak Pro; the first lessons are free.