Sub topic 7
How ADD_MONTHS Handles Month Ends
This chapter assumes you know basic SELECT statements, the DUAL table, WHERE clauses, and Oracle date literals such as DATE '2024-01-31'. The examples use TO_CHAR so that the results do not depend on your session's date display format.
ADD_MONTHS shifts an Oracle DATE by a specified number of calendar months:
ADD_MONTHS(date_value, number_of_months)The second argument can be positive or negative. A positive value moves forward. A negative value moves backward. The function returns a DATE, including the original time-of-day portion.
The important rule is that Oracle preserves the day of the month when possible. If the target month does not contain that day, Oracle uses the target month's last day. Also, if the input date is already the last day of its month, the result is the last day of the target month.
The end-of-month rule
Consider these examples:
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:
| Input and operation | Result | Reason |
|---|---|---|
| January 15 plus one month | 2024-02-15 | February has day 15 |
| January 31 plus one month | 2024-02-29 | February has no day 31 |
| February 29 plus one month | 2024-03-31 | February 29 is the month's last day |
| February 29 plus twelve months | 2025-02-28 | The target year is not a leap year |
| March 31 minus one month | 2024-02-29 | March 31 is a month end |
The function does not simply add a fixed number of days. A month can have 28, 29, 30, or 31 days, so month arithmetic needs calendar rules.
Predicting results before running a query
Use this process:
- Move the input date by the requested number of months.
- Check whether the original date is the last day of its month.
- Check whether the target month contains the original day number.
- Use the target month's last day when either month-end rule applies.
For example, ADD_MONTHS(DATE '2024-01-30', 1) returns February 29 because February has no day 30. ADD_MONTHS(DATE '2024-01-29', 1) also returns February 29 in leap year 2024. In 2025, the same expression returns February 28.
A time component remains attached to the date:
SELECT TO_CHAR(
ADD_MONTHS(
TO_DATE('2024-01-31 14:45:00', 'YYYY-MM-DD HH24:MI:SS'),
1
),
'YYYY-MM-DD HH24:MI:SS'
) AS shifted_datetime
FROM dual;This returns 2024-02-29 14:45:00.
Why repeated month shifts can surprise you
Month-end behavior can affect repeated calculations. Starting with January 31, adding one month produces February 29. Adding another month produces March 31 because February 29 is itself a month end:
SELECT TO_CHAR(ADD_MONTHS(DATE '2024-01-31', 1), 'YYYY-MM-DD') AS first_shift,
TO_CHAR(ADD_MONTHS(ADD_MONTHS(DATE '2024-01-31', 1), 1),
'YYYY-MM-DD') AS second_shift
FROM dual;The results are 2024-02-29 and 2024-03-31. If your business rule requires a fixed day when possible, calculate from the original anchor date rather than repeatedly shifting the previous result.
Locale and related choices
Oracle defines a month according to the session's NLS_CALENDAR setting. Most databases use the Gregorian calendar, but applications that support other calendar systems should account for this setting.
Use ADD_MONTHS when you need calendar-month behavior, such as billing dates or monthly renewals. Use an interval when you need explicit interval semantics, especially with TIMESTAMP values:
SELECT DATE '2024-01-15' + INTERVAL '1' MONTH AS one_month_later
FROM dual;The choice depends on the datatype and the calendar rules your application requires.
Exercise
What does ADD_MONTHS(DATE '2024-01-31', 1) return in the leap year 2024, and why?
Show answer
It returns 2024-02-29 because January 31 is a month-end date and February 2024 ends on February 29.