# How to get the last day of the month in SQL

Source: https://sqlstreak.com/sql/how-to/last-day-of-month · Tested on PostgreSQL 18.6 and SQLite 3.54.1; results generated 2026-11-22.


**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:

```sql
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:

```sql
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 (both engines):

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

### How many days a month has

The day number of the last day. February 2024 was a leap month.

PostgreSQL:

```sql
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:

```sql
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 (both engines):

| any_day | days_in_month |
| --- | --- |
| 2024-02-10 | 29 |
| 2026-02-10 | 28 |
| 2026-04-30 | 30 |

## How it works

- `date_trunc('month', d)`: PostgreSQL: back to midnight on the 1st of the month.
- `+ INTERVAL '1 month - 1 day'`: Forward to the 1st of the next month, then back one day.
- `date(d, 'start of month', '+1 month', '-1 day')`: SQLite: the same three moves as modifiers, applied left to right.

## 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.
