Sub topic 6
Shifting Dates with `ADD_MONTHS`
This chapter assumes you know basic SELECT statements, the WHERE clause, the DUAL table, and Oracle DATE literals. You should also know that an Oracle DATE stores both a calendar date and a time of day.
Why use `ADD_MONTHS`?
ADD_MONTHS shifts a date by a number of calendar months:
ADD_MONTHS(date_value, month_count)The second argument can be positive or negative. A positive value moves forward. A negative value moves backward. Unlike adding a fixed number of days, ADD_MONTHS follows calendar-month rules.
For example, adding one month to January 15 produces February 15. Adding one month to January 31 requires a special rule because February does not always have 31 days.
SELECT TO_CHAR(ADD_MONTHS(DATE '2024-01-15', 1), 'YYYY-MM-DD') AS shifted_date,
TO_CHAR(ADD_MONTHS(DATE '2024-01-15', -2), 'YYYY-MM-DD') AS earlier_date
FROM dual;The results are 2024-02-15 and 2023-11-15.
ADD_MONTHS returns an Oracle DATE. If you pass a TIMESTAMP, Oracle converts the result to DATE, so fractional seconds are not preserved.
The end-of-month rule
Oracle applies this rule:
- If the input date is the last day of its month, the result is the last day of the target month.
- If the target month has no matching day number, the result is the last day of the target month.
- Otherwise, the result keeps the same day number.
SELECT TO_CHAR(ADD_MONTHS(DATE '2024-01-15', 1), 'YYYY-MM-DD') AS normal_case,
TO_CHAR(ADD_MONTHS(DATE '2024-01-31', 1), 'YYYY-MM-DD') AS target_too_short,
TO_CHAR(ADD_MONTHS(DATE '2024-02-29', 1), 'YYYY-MM-DD') AS input_month_end,
TO_CHAR(ADD_MONTHS(DATE '2024-03-31', -1), 'YYYY-MM-DD') AS previous_month
FROM dual;| Expression | 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 | The input is a month end |
| March 31 minus one month | 2024-02-29 | The input is a month end |
The last-day behavior can sometimes surprise you. Applying ADD_MONTHS repeatedly does not always return the same day number:
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 first result is February 29. Because February 29 is the last day of its month, the second shift produces March 31.
Using `ADD_MONTHS` in queries
ADD_MONTHS is useful for subscription dates, renewal dates, billing periods, and retention windows.
SELECT subscription_id,
start_date,
ADD_MONTHS(start_date, 12) AS renewal_date
FROM subscriptions;This calculates a date one calendar year after start_date. A start date on the last day of a month produces a renewal date on the last day of the corresponding month.
You can also calculate a rolling boundary from the current date:
SELECT order_id,
order_date
FROM orders
WHERE order_date >= ADD_MONTHS(TRUNC(SYSDATE), -3);TRUNC(SYSDATE) removes the current time before the calculation. Without it, the boundary includes the current time of day, which may exclude rows from the boundary date.
Exercise
What does ADD_MONTHS(DATE '2024-01-31', 1) return, and why?
Show answer
It returns 2024-02-29 because February 2024 has no day 31, so Oracle uses the last day of the target month.