sqlstreak

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

namehired_onmonth_startmonth_end
Rosa Vega2024-01-102024-01-012024-01-31
Omar Haddad2024-03-012024-03-012024-03-31
Ben Carter2024-04-202024-04-012024-04-30
Lia Moreau2024-06-152024-06-012024-06-30
Ivan Petrov2024-08-012024-08-012024-08-31
Yuki Tanaka2025-02-012025-02-012025-02-28
Zara Ali2025-05-122025-05-012025-05-31
Noah Kim2025-09-302025-09-012025-09-30

Try it: PostgreSQL playground · SQLite playground

How it works

  1. 1
    date_trunc('month', d)

    PostgreSQL: back to midnight on the 1st of the month.

  2. 2
    + INTERVAL '1 month - 1 day'

    Forward to the 1st of the next month, then back one day.

  3. 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_daydays_in_month
2024-02-1029
2026-02-1028
2026-04-3030

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_day against 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

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.

Start free