Sub topic 7
How ADD_MONTHS handles month ends
This chapter assumes you can write basic SELECT queries, use the DUAL table, and call ADD_MONTHS for simple cases. It covers the one part of ADD_MONTHS that surprises people: what happens when the starting date is at or near the end of a month.
The two rules
ADD_MONTHS(date, n) moves a date forward (or backward, if n is negative) by n calendar months. It applies two rules, in this order:
- If the input date is the last day of its month, the result is the last day of the target month.
- Otherwise, the result keeps the same day of month. If the target month is too short to have that day, the result is the last day of the target month.
If neither rule applies, the day of month is unchanged. Rule 1 is the one that catches people. It means ADD_MONTHS does not simply clamp a day that does not exist. It also treats "last day of the month" as a position that it preserves.
Working through the cases
This query runs five cases and formats each result with TO_CHAR, so the output does not depend on your session's NLS_DATE_FORMAT.
SELECT TO_CHAR(ADD_MONTHS(DATE '2024-01-15', 1), 'YYYY-MM-DD') AS plain_case,
TO_CHAR(ADD_MONTHS(DATE '2024-01-31', 1), 'YYYY-MM-DD') AS clamped_to_month_end,
TO_CHAR(ADD_MONTHS(DATE '2024-02-29', 1), 'YYYY-MM-DD') AS last_day_stays_last_day,
TO_CHAR(ADD_MONTHS(DATE '2024-02-29', 12), 'YYYY-MM-DD') AS leap_day_plus_a_year,
TO_CHAR(ADD_MONTHS(DATE '2024-03-31', -1), 'YYYY-MM-DD') AS going_backwards
FROM dual;The results are 2024-02-15, 2024-02-29, 2024-03-31, 2025-02-28, and 2024-02-29. The table below explains each one, and adds a few more cases.
| Input | n | Result | Reason |
|---|---|---|---|
| 2024-01-15 | 1 | 2024-02-15 | Day 15 exists in February. |
| 2024-01-31 | 1 | 2024-02-29 | February 2024 has no day 31, so the result is its last day. |
| 2025-01-30 | 1 | 2025-02-28 | February 2025 has no day 30, so the result is its last day. |
| 2024-02-29 | 1 | 2024-03-31 | The input is a month end, so the result is a month end (rule 1). |
| 2024-02-29 | 12 | 2025-02-28 | The input is a month end, and 2025-02-28 is the last day of February 2025. |
| 2024-04-30 | 1 | 2024-05-31 | April 30 is a month end, so the result is May 31, not May 30. |
| 2025-02-28 | 1 | 2025-03-31 | February 28, 2025 is a month end, so the result is March 31. |
| 2024-03-31 | -1 | 2024-02-29 | Day 31 does not exist in February, so the result is its last day. |
The row for 2024-04-30 is the one to remember. A date that is a month end in a 30-day month lands on day 31 in a 31-day month. The same happens for 2025-02-28 moving to March 31.
The rule looks at the input date, not at the calendar in general. ADD_MONTHS(DATE '2024-02-28', 1) returns 2024-03-28, because February 28 is not the last day of February in a leap year. In 2025 the same day and month returns March 31.
The time of day is kept
ADD_MONTHS changes only the month, year, and (when needed) the day. The time portion of the DATE passes through unchanged.
SELECT TO_CHAR(
ADD_MONTHS(TO_DATE('2024-01-31 15:45:10', 'YYYY-MM-DD HH24:MI:SS'), 1),
'YYYY-MM-DD HH24:MI:SS') AS shifted
FROM dual;The result is 2024-02-29 15:45:10. The month-end check is based on the date part, so a month-end date with a time of day still triggers rule 1.
If you pass a TIMESTAMP value, Oracle converts it to a DATE first, so fractional seconds and any time zone are lost. The return type is always DATE.
ADD_MONTHS is not reversible
Because of rule 1, adding a month and then subtracting it does not always give back the original date.
SELECT TO_CHAR(DATE '2024-01-30', 'YYYY-MM-DD') AS start_date,
TO_CHAR(ADD_MONTHS(DATE '2024-01-30', 1), 'YYYY-MM-DD') AS plus_one,
TO_CHAR(ADD_MONTHS(ADD_MONTHS(DATE '2024-01-30', 1), -1),
'YYYY-MM-DD') AS and_back
FROM dual;The output is 2024-01-30, 2024-02-29, and 2024-01-31. January 30 moved to February 29, which is the last day of February. Moving back one month then follows rule 1 and returns the last day of January, which is the 31st. The date drifted by one day.
Note that this is not an error. It is the documented behavior, and it is consistent: the function preserves "month end" rather than the exact day number.
Chained calls differ from a single call
Repeated one-month steps can give a different answer from one multi-month step. Take an anchor of 2024-01-30 and compare the two approaches. The first query computes every date directly from the anchor.
SELECT LEVEL - 1 AS n,
TO_CHAR(ADD_MONTHS(DATE '2024-01-30', LEVEL - 1), 'YYYY-MM-DD') AS from_anchor
FROM dual
CONNECT BY LEVEL <= 5;This returns 2024-01-30, 2024-02-29, 2024-03-30, 2024-04-30, and 2024-05-30 for n = 0 to 4. The second query adds one month to the previous row's result each time.
WITH series (n, d) AS (
SELECT 0, DATE '2024-01-30' FROM dual
UNION ALL
SELECT n + 1, ADD_MONTHS(d, 1) FROM series WHERE n < 4
)
SELECT n, TO_CHAR(d, 'YYYY-MM-DD') AS chained
FROM series
ORDER BY n;This returns 2024-01-30, 2024-02-29, 2024-03-31, 2024-04-30, and 2024-05-31. Once the chain passes through February 29, which is a month end, every later step is a month end too.
For recurring schedules such as billing dates, always compute from the original anchor with ADD_MONTHS(anchor, n). That keeps day 30 as day 30 in the months that have it, and clamps only in the months that do not.
Detecting a month-end input
If your logic needs to know whether rule 1 will apply, compare the date with LAST_DAY. LAST_DAY keeps the time portion, so the comparison works even when the value has a time of day.
SELECT d,
CASE WHEN d = LAST_DAY(d) THEN 'month end' ELSE 'not month end' END AS kind
FROM (SELECT DATE '2024-02-29' AS d FROM dual
UNION ALL
SELECT DATE '2024-02-28' FROM dual
UNION ALL
SELECT DATE '2025-02-28' FROM dual);February 29, 2024 and February 28, 2025 are month ends. February 28, 2024 is not. The d column displays using your session's date format, which is why the earlier examples used TO_CHAR.
Fractional values of n
The second argument is expected to be an integer. If you pass a fractional value, Oracle truncates the fraction toward zero before shifting. ADD_MONTHS(DATE '2024-01-15', 1.9) shifts by one month, not two. Do not use fractions to mean "part of a month". If you need to move a date by days, use day arithmetic from the earlier chapter.
Comparing with interval arithmetic
You can also add months using an interval literal:
SELECT DATE '2024-01-15' + INTERVAL '1' MONTH FROM dual;This works for 2024-01-15 and returns 2024-02-15. With a start of 2024-01-31, the same expression raises ORA-01839: date not valid for month specified, because the result would be February 31. ADD_MONTHS never raises that error. It clamps instead. That makes ADD_MONTHS the safer choice for general calendar math, but it also means you should know the clamping and month-end rules above, since they are applied silently.
Common mistakes
- Assuming the day number is preserved.
ADD_MONTHS(DATE '2024-04-30', 1)gives May 31, not May 30. - Building a monthly series by repeatedly feeding results back in. Compute each date from the anchor instead.
- Expecting
ADD_MONTHS(ADD_MONTHS(d, 1), -1)to equald. It is only guaranteed whendis neither a month end nor a date that gets clamped. - Printing results without
TO_CHARand mistaking a display format for a different value. The stored value is the same; only the formatting differs. - Passing a
TIMESTAMPand expecting fractional seconds to survive.
Exercise
What do ADDMONTHS(DATE '2025-02-28', 1) and ADDMONTHS(DATE '2024-02-28', 1) return, and why do they differ?
Show answer
The first returns 2025-03-31 and the second returns 2024-03-28. In 2025, February 28 is the last day of the month, so rule 1 makes the result the last day of March. In 2024 (a leap year), February 28 is not the last day, so the day number 28 is kept.
Exercise
A subscription starts on 2024-01-31. Write a query that returns the billing date for months 0 to 3 so that no date drifts, and state the four dates it returns.
Show answer
SELECT LEVEL - 1 AS n, TOCHAR(ADDMONTHS(DATE '2024-01-31', LEVEL - 1), 'YYYY-MM-DD') AS bill_date FROM dual CONNECT BY LEVEL <= 4; It returns 2024-01-31, 2024-02-29, 2024-03-31, and 2024-04-30. Each date is computed from the anchor rather than from the previous row.
Exercise
Starting from DATE '2024-01-30', what does ADDMONTHS(ADDMONTHS(DATE '2024-01-30', 1), 1) return, and how does it compare with ADD_MONTHS(DATE '2024-01-30', 2)?
Show answer
The chained call returns 2024-03-31, because the first step gives 2024-02-29, a month end, and the second step then returns the last day of March. The single call returns 2024-03-30, because January 30 is not a month end and March has a day 30.