In SQL, what does DATE '2024-01-31' + INTERVAL '1' MONTH return, and why does a monthly schedule built by repeated addition drift?
answer
- 31 February does not exist
- clamp, error or roll over
- the result forgets the 31st
- add from a fixed anchor
- first of next month, minus one day
basics
~20 sPostgreSQL and MySQL clamp 31 January plus a month to 29 February 2024, and the lost day never returns: a second month gives 29 March. Compute each date from the original anchor, not the previous result.
solid answer
~50 sThe answer depends on the engine, which is why interviewers ask it. PostgreSQL and MySQL clamp an impossible day to the month's last valid day, so the result is 29 February 2024. Oracle's interval addition raises an error instead, because 31 February does not exist, and SQLite's date modifier rolls over to 2 March. Clamping loses information: the result no longer remembers the 31st, so adding another month gives 29 March, while adding two months at once gives 31 March. A schedule that adds a month to its last value therefore settles on the 28th or 29th and stays there. Compute each occurrence from a fixed anchor instead, with `anchor + n * INTERVAL '1' MONTH` on PostgreSQL or `ADD_MONTHS(anchor, n)` on Oracle. For a month's last day, go to the first of the next month and subtract one day.
code
sql · 5 linesSELECT DATE '2024-01-31' + INTERVAL '1' MONTH + INTERVAL '1' MONTH;
-- 2024-03-29 00:00:00
SELECT DATE '2024-01-31' + 2 * INTERVAL '1' MONTH;
-- 2024-03-31 00:00:00go deeper
Recall that a month has no fixed length, so a month interval is not 30 days. Write a simple date plus INTERVAL expression and say what happens on PostgreSQL when the target day does not exist.
Explain clamping and what it loses: chained additions drift while a single larger addition does not. Name the engines that differ, with Oracle's interval addition raising an error and SQLite rolling over.
Spot schedules and billing rows that add to their previous value, and move them to an anchor plus a counter. Look for quiet drift by checking dates that were meant to be month ends.
Treat month-end behaviour as a business rule that needs an owner, for billing especially. Decide what the 31st means in a short month and put one tested implementation behind it, rather than letting each engine's arithmetic decide.
## What an interval holds An **interval** is a span of time written in calendar units. `INTERVAL '1' MONTH` does not mean a fixed number of days, because months differ in length, and PostgreSQL makes this explicit in how it stores the value: three separate fields for **months, days and microseconds**. When you add an interval to a timestamp, PostgreSQL applies the months first, then the days, then the microseconds. Two PostgreSQL results show the difference: - `TIMESTAMP '2024-02-01' + INTERVAL '1' MONTH` gives 1 March, and `TIMESTAMP '2024-02-01' + INTERVAL '30' DAY` gives 2 March. - `INTERVAL '1' MONTH = INTERVAL '30' DAY` is true when compared directly, since comparison treats a month as 30 days, yet the two additions above land on different dates. ## What month-end addition returns Adding one month to 31 January asks for 31 February, and engines answer that differently: | Engine | Expression | Result | |---|---|---| | PostgreSQL | `DATE '2024-01-31' + INTERVAL '1' MONTH` | `2024-02-29 00:00:00`, a timestamp | | MySQL | `DATE '2024-01-31' + INTERVAL 1 MONTH` | `2024-02-29` | | Oracle | `DATE '2024-01-31' + INTERVAL '1' MONTH` | an error, the date is not valid for the month | | Oracle | `ADD_MONTHS(DATE '2024-01-31', 1)` | 29 February | | SQLite | `date('2024-01-31', '+1 month')` | `2024-03-02`, the overflow rolls forward | The PostgreSQL manual states its rule directly: the day of the month stays the same unless it would be past the end of the new month, in which case the last day of that month is used. Its own example is that 31 March plus 1 month becomes 30 April, while 31 March plus 2 months becomes 31 May. Notice also that `date + interval` is a `timestamp` on PostgreSQL. Cast it back with `::date` when a date is what you meant to return. ## Why repeated addition drifts Clamping loses information, because the result keeps no record of the day you started from. Trace a schedule that stores `next_run` and adds a month to it each time on PostgreSQL, starting on 31 January 2024: 1. `2024-01-31` plus a month gives `2024-02-29`, clamped. 2. `2024-02-29` plus a month gives `2024-03-29`, since 29 March exists and nothing recalls the 31st. 3. Every later month keeps the 29th, until February 2025 clamps it to the 28th, where it stays. So a bill meant for the last day of each month slides to the 28th within about a year, without an error and without any row that looks wrong on its own. The same loss makes the arithmetic **not reversible**. `DATE '2024-03-31' - INTERVAL '1' MONTH + INTERVAL '1' MONTH` returns 29 March, not 31 March. It is also why two steps differ from one: `+ INTERVAL '1' MONTH + INTERVAL '1' MONTH` gives 29 March, and `+ INTERVAL '2' MONTH` gives 31 March. ## The fixes - **Keep an anchor.** Store the original date and compute occurrence `n` from it: `anchor + n * INTERVAL '1' MONTH` on PostgreSQL, `ADD_MONTHS(anchor, n)` on Oracle, and `anchor + INTERVAL n MONTH` on MySQL. From 31 January this gives 29 February, 31 March, 30 April, 31 May. - **Compute a month's last day from its first.** The first of a month always exists, so `first_of_month + INTERVAL '1' MONTH - INTERVAL '1' DAY` works on any engine with interval addition. Going through `d + INTERVAL '1' MONTH - INTERVAL '1' DAY` does not: from 31 January 2024 it gives 28 February, because the clamp happens first and then a day comes off. - **Use the first of the next month as a range bound.** A month's rows are `ts >= month_start AND ts < month_start + INTERVAL '1' MONTH`. The upper bound never needs the last day at all, so month length stops mattering. ## Days and hours are different units too On a `timestamp with time zone`, PostgreSQL keeps the local time of day when adding months or days, and adds microseconds literally. With the session zone set to `America/New_York`, the clocks change on 10 March 2024, so: - `TIMESTAMPTZ '2024-03-09 12:00' + INTERVAL '1 day'` gives `2024-03-10 12:00:00-04`. - `TIMESTAMPTZ '2024-03-09 12:00' + INTERVAL '24 hours'` gives `2024-03-10 13:00:00-04`. Choose the unit by meaning: a daily job at noon wants `1 day`, and a 24-hour token expiry wants `24 hours`. ## Subtracting is a separate question On PostgreSQL, `date - date` returns an integer count of days, and `timestamp - timestamp` returns an interval. `DATE '2024-03-01' - DATE '2024-02-01'` is 29. Code that expects one result type where the other arrives breaks at the next arithmetic step, so check what your engine returns.
- Why doesn't subtracting a month and adding it back restore the original date on PostgreSQL?Each step can clamp, and a clamped result has forgotten its day. 31 March minus a month is 29 February 2024, and adding a month gives 29 March. So month arithmetic is not reversible, and a query should not rely on it to recover an original date. Keep the original value instead.
- On PostgreSQL, does adding INTERVAL '1 day' to a timestamp with time zone equal adding INTERVAL '24 hours'?Not across a clock change. A day keeps the local time of day, and hours are added literally. In a New York session, noon on 9 March 2024 plus a day is noon on 10 March, while plus 24 hours is 13:00, because the clocks skip an hour in between.
- An INTERVAL of one month always adds exactly 30 days.
- Adding a month and then subtracting a month always returns the original date.
- Adding one month twice gives the same date as adding two months at once.
- Every engine clamps 31 January plus one month to the end of February.
- Adding INTERVAL '1 day' and adding INTERVAL '24 hours' always produce the same timestamp.