Sub topic 6
Shifting dates with ADD_MONTHS
This chapter assumes you know that an Oracle DATE always carries a time of day and that SYSDATE returns the current server date and time. It also assumes you can add whole days with date + n. You should be comfortable with SELECT ... FROM dual and TO_CHAR for display.
Why day arithmetic is not enough
Adding 30 to a date moves it forward 30 days. Calendar months are not 30 days long, though. A subscription that renews "one month after it starts" should land on the same day of the next month, whether that month has 28, 29, 30, or 31 days.
ADD_MONTHS does this. It moves a date forward or backward by a whole number of calendar months and returns a DATE.
ADD_MONTHS(date_value, n)date_valueis aDATE, or any value Oracle can implicitly convert to one.nis the number of months to shift. A negativenmoves backward.- The result is always a
DATE, even if you pass aTIMESTAMP.
The basic behavior
SELECT TO_CHAR(ADD_MONTHS(DATE '2024-01-15', 1), 'YYYY-MM-DD') AS next_month,
TO_CHAR(ADD_MONTHS(DATE '2024-01-15', 3), 'YYYY-MM-DD') AS next_quarter,
TO_CHAR(ADD_MONTHS(DATE '2024-01-15', 12), 'YYYY-MM-DD') AS next_year,
TO_CHAR(ADD_MONTHS(DATE '2024-01-15', -2), 'YYYY-MM-DD') AS two_months_back
FROM dual;This returns 2024-02-15, 2024-04-15, 2025-01-15, and 2023-11-15.
There is no ADD_YEARS function. Multiply by 12 instead, so ADD_MONTHS(d, 12 * 5) is five years later. Quarters are 3 * n.
What happens inside the call
ADD_MONTHS works on the calendar fields of the date, not on a day count. It shifts the month, carries into the year when needed, and then fixes up the day of month if it does not exist in the target month.
The decision in the middle is the month-end rule. The next chapter covers it in full, including the case where the input is itself the last day of a month. For now, remember that ADD_MONTHS never raises an error for a missing day such as February 30. It clamps the day instead.
The time of day is preserved
ADD_MONTHS changes only the month and year (and the day, when clamping). It does not touch the hours, minutes, or seconds.
SELECT TO_CHAR(
ADD_MONTHS(TO_DATE('2024-01-15 14:30:00', 'YYYY-MM-DD HH24:MI:SS'), 1),
'YYYY-MM-DD HH24:MI:SS') AS shifted
FROM dual;The result is 2024-02-15 14:30:00.
This matters when you compare shifted values against columns that hold times. If you want midnight, apply TRUNC first: ADD_MONTHS(TRUNC(SYSDATE), 1).
Input types and edge cases
| Input | Result |
|---|---|
| `DATE` | `DATE`, time of day kept |
| `TIMESTAMP` or `TIMESTAMP WITH TIME ZONE` | `DATE`, fractional seconds and time zone dropped |
| `NULL` date or `NULL` months | `NULL` |
| Fractional `n`, such as `1.9` | The fraction is truncated, so `1.9` behaves as `1` |
| Character string | Implicitly converted using `NLS_DATE_FORMAT`; avoid this |
The last row is a common source of bugs. A call like ADD_MONTHS('15-JAN-24', 1) works on one session and fails on another, because the string is parsed with the session's date format and language. Write a DATE literal or use TO_DATE with an explicit format model.
SELECT TO_CHAR(ADD_MONTHS(TIMESTAMP '2024-01-15 14:30:00.123456', 1),
'YYYY-MM-DD HH24:MI:SS') AS from_timestamp,
TO_CHAR(ADD_MONTHS(DATE '2024-01-15', 1.9),
'YYYY-MM-DD') AS fractional_n,
ADD_MONTHS(CAST(NULL AS DATE), 1) AS null_in_null_out
FROM dual;The first column is 2024-02-15 14:30:00, the second is 2024-02-15, and the third is NULL.
ADD_MONTHS versus adding days
The two approaches answer different questions.
| Goal | Use | Why |
|---|---|---|
| Same calendar day next month | `ADD_MONTHS(d, 1)` | Follows the real month length |
| Exactly 30 days later | `d + 30` | Fixed number of 24-hour days |
| Exactly one week later | `d + 7` | Weeks have a fixed length |
| A date a year later | `ADD_MONTHS(d, 12)` | Handles leap years |
Adding 365 for "one year" drifts by a day in leap years. Adding 30 for "one month" drifts further every month.
Using a column as the month count
The month count can be any numeric expression, including a column. This is the usual pattern for contracts, loans, and warranties.
WITH contracts AS (
SELECT 1 AS contract_id, DATE '2024-01-15' AS start_date, 6 AS term_months FROM dual
UNION ALL
SELECT 2, DATE '2024-08-31', 6 FROM dual
UNION ALL
SELECT 3, DATE '2023-11-30', 3 FROM dual
)
SELECT contract_id,
TO_CHAR(start_date, 'YYYY-MM-DD') AS start_date,
term_months,
TO_CHAR(ADD_MONTHS(start_date, term_months), 'YYYY-MM-DD') AS renewal_date
FROM contracts
ORDER BY contract_id;The renewal dates are 2024-07-15, 2025-02-28, and 2024-02-29. Contracts 2 and 3 start on the last day of a month, so their renewals land on the last day of the target month. That is why contract 3 renews on a leap day.
If a contract should end the day before the renewal date, subtract one day after shifting: ADD_MONTHS(start_date, term_months) - 1. Do the subtraction last, because day arithmetic on the input first would change which day ADD_MONTHS sees.
Building month boundaries
ADD_MONTHS combines well with TRUNC(date, 'MM'), which returns the first day of the month at midnight. The second chapter-length topic on companion functions covers format models properly. Here you only need that one form.
A reliable filter for "the current calendar month" uses a half-open range, meaning the start is inclusive and the end is exclusive:
SELECT order_id, order_date
FROM orders
WHERE order_date >= TRUNC(SYSDATE, 'MM')
AND order_date < ADD_MONTHS(TRUNC(SYSDATE, 'MM'), 1);The upper bound is midnight on the first day of next month. Because ADD_MONTHS starts from the first of a month, the month-end clamping never applies, and the result is correct for months of any length. This avoids hard-coding 28, 29, 30, or 31, and it avoids the trap of <= last_day which would drop rows after midnight on the last day.
The same pattern gives "the previous month": ADD_MONTHS(TRUNC(SYSDATE, 'MM'), -1) through TRUNC(SYSDATE, 'MM').
Common errors and gotchas
- Chaining is not the same as one big shift.
ADD_MONTHS(ADD_MONTHS(DATE '2024-01-30', 1), 1)returns2024-03-31, butADD_MONTHS(DATE '2024-01-30', 2)returns2024-03-30. The first step clamps to the last day of February, and that value is then treated as a month end. When you need to move by several months, shift once from the original date. - Round trips are not guaranteed. Going forward and back by the same count may not return the original date when clamping occurred along the way.
- Passing
nas a non-numeric string.ADD_MONTHS(d, '1')relies on implicit conversion and is fragile. Pass a number. - Expecting a
TIMESTAMPback. If you need fractional seconds or a time zone after shifting,ADD_MONTHSis the wrong tool. The last chapter covers interval arithmetic for that case. - Comparing a shifted date to a date with a time. If
order_datehas times, a condition likeorder_date = ADD_MONTHS(TRUNC(SYSDATE), -1)matches only rows at exactly midnight. Use a range.
Summary
ADD_MONTHS(d, n)shifts byncalendar months, forward or backward, and returns aDATE.- It keeps the time of day, drops
TIMESTAMPprecision, truncates a fractionaln, and returnsNULLforNULLinput. - It never fails on a missing day; it clamps to the last day of the target month.
- Prefer one shift from the original date over chained shifts.
- Use first-of-month plus
ADD_MONTHSto build clean half-open ranges.
Exercise
A table subscriptions(sub_id, start_date, term_months) stores start dates with a time of day. Write a query that returns each subscription's renewal date as midnight at the start of the renewal day, and then state what ADD_MONTHS(DATE '2024-05-31', 1) and ADD_MONTHS(DATE '2024-05-30', 1) return.
Show answer
Use SELECT sub_id, TRUNC(ADD_MONTHS(start_date, term_months)) AS renewal_date FROM subscriptions;. ADD_MONTHS keeps the time of day, and TRUNC removes it. ADD_MONTHS(DATE '2024-05-31', 1) returns 2024-06-30, because June has no 31st. ADD_MONTHS(DATE '2024-05-30', 1) also returns 2024-06-30, because the 30th exists in June.
Exercise
Write a WHERE clause that selects rows from orders placed in the previous calendar month, whatever time of day they were placed. Explain why order_date BETWEEN ADD_MONTHS(TRUNC(SYSDATE, 'MM'), -1) AND TRUNC(SYSDATE, 'MM') is a bad choice.
Show answer
Use WHERE order_date >= ADD_MONTHS(TRUNC(SYSDATE, 'MM'), -1) AND order_date < TRUNC(SYSDATE, 'MM'). The BETWEEN version is inclusive at both ends, so it also returns rows stamped exactly midnight on the first day of the current month, which belong to the current month, not the previous one.