Query
The teaching database contains orders from May and June 2025. SQLite can move a date to the start of its month with the modifier 'start of month'. To get the last day, move forward one month and back one day.
SELECT id,
date,
date(date, 'start of month') AS month_start,
date(date, 'start of month', '+1 month', '-1 day') AS month_end
FROM orders
WHERE id IN (101, 111)
ORDER BY id;The same pattern works in February without having to remember whether it is a leap year: the engine works out the start of the next month and then subtracts a day.
How it works
truncated_to_month is not a universal SQL function. It stands for whatever operation returns the start of the month on your engine. For the end, some engines offer a direct function; others start from the next month and go back one day.
Cases that change the result
The the last day is useful for displaying periods, but a filter on timestamps needs more care. If you write a bound equivalent to 2025-05-31 00:00:00, you can leave out almost all of 31 May. Making up 23:59:59 is not robust either when values have fractions of a second.
A safer approach is a half-open interval: include the start of the month and exclude the start of the next.
WHERE date >= month_start
AND date < next_month_startWith plain dates, an inclusive range may be enough. With timestamps, the interval >= start and < next_month covers the whole period without depending on the engine's time precision.
Avoid this confusion
Subtracting 30 days to find the previous month
Months have no fixed length. February, April and July cannot all be modelled as “30 days”. Work with the engine's calendar units.
Confusing the last day with the last moment
2025-05-31 represents a day; on its own it does not say down to which fraction of a second a timestamp filter should accept. For monthly filters, prefer the start of the next month as the exclusive bound.
Copying a function from one engine to another
EOMONTH, LAST_DAY, date_trunc and SQLite's modifiers are not one portable syntax. Keep the mental model and adapt the expression to the dialect you actually use.
Key differences between engines
PostgreSQL: date_trunc + interval
date_trunc('month', date) moves the value to the start of the month. To get a date with no time you can cast the result to date. The last day comes from moving forward one month and subtracting one day.
SELECT date_trunc('month', date)::date AS month_start,
(date_trunc('month', date)
+ INTERVAL '1 month'
- INTERVAL '1 day')::date AS month_end
FROM orders;If date is timestamp with time zone, the time zone used for truncation can change which moment falls on which day. Decide on the business time zone before building periods.
MySQL: DATE_FORMAT + LAST_DAY
MySQL offers LAST_DAY() for the end of the month. For the start, one explicit approach is to build day 01 with DATE_FORMAT and convert it back to DATE.
SELECT CAST(DATE_FORMAT(date, '%Y-%m-01') AS DATE) AS month_start,
LAST_DAY(date) AS month_end
FROM orders;DATE_FORMAT produces formatted text; the CAST makes it clear that the final result should be treated as a date again.
SQL Server 2022+: DATETRUNC + EOMONTH
SQL Server 2022 introduced DATETRUNC, which can truncate to the month. EOMONTH returns the last day of the month directly and is also available in older versions.
SELECT CAST(DATETRUNC(month, date) AS date) AS month_start,
EOMONTH(date) AS month_end
FROM orders;In SQL Server before 2022 you can build the start with DATEFROMPARTS(YEAR(date), MONTH(date), 1) and keep EOMONTH(date) for the end.
SQLite: modifiers for date()
SQLite applies date modifiers from left to right. 'start of month' moves back to the start of the month; '+1 month' and '-1 day' land on the last day of the original month.