Sub topic 8
Measuring distance with MONTHS_BETWEEN
This chapter assumes you can write basic SELECT queries with a WHERE clause, use the DUAL table, and know that an Oracle DATE carries a time of day. You have seen ADD_MONTHS and its month-end rule. MONTHS_BETWEEN is the inverse question: given two dates, how many months apart are they?
The function and its sign
MONTHS_BETWEEN(date1, date2) returns a NUMBER equal to date1 minus date2, measured in months.
- If
date1is later thandate2, the result is positive. - If
date1is earlier, the result is negative. - If either argument is NULL, the result is NULL.
SELECT MONTHS_BETWEEN(DATE '2024-03-15', DATE '2024-01-15') AS whole_months,
MONTHS_BETWEEN(DATE '2024-03-31', DATE '2024-02-29') AS both_month_ends,
MONTHS_BETWEEN(DATE '2024-02-10', DATE '2024-01-01') AS fractional,
MONTHS_BETWEEN(DATE '2024-01-01', DATE '2024-02-10') AS negative_result
FROM dual;| Column | Result | Why |
|---|---|---|
| whole_months | 2 | Both dates fall on day 15. |
| both_month_ends | 1 | Both dates are the last day of their month. |
| fractional | 1.2903225806 | One month plus 9 days, with each extra day worth 1/31. |
| negative_result | -1.2903225806 | The same pair with the arguments swapped. |
Always put the later date first when you want a positive number. Swapping the arguments only flips the sign, so a reversed call gives a wrong answer without raising an error.
Pass real DATE values, either DATE '...' literals or TO_DATE with an explicit format. If you pass a character string, Oracle converts it using the session's NLS_DATE_FORMAT. That conversion fails or silently misreads the date when the format differs between sessions.
The two cases the function distinguishes
Oracle first decides whether the pair is "aligned". Aligned pairs get an exact integer. All other pairs get a fraction built on a 31-day month.
flowchart TD
A[Two dates: date1 and date2] --> B{Same day of month?}
B -- yes --> E[Integer result, time of day ignored]
B -- no --> C{Both are the last day of their month?}
C -- yes --> E
C -- no --> D[Whole months from year and month difference, plus day and time difference divided by 31]The decision flowchart above summarizes the rule. The Oracle documentation states it this way: if both dates fall on the same day of the month, or both are last days of their months, the result is an integer. Otherwise Oracle computes the fractional part from a 31-day month, and that part includes the time of day.
The 31-day rule, step by step
For a non-aligned pair, the result is:
(year1 - year2) * 12 + (month1 - month2) + (day1 - day2 + time difference as a fraction of a day) / 31Oracle divides by 31 for every month, including February and 30-day months. You can reproduce the fractional case with plain arithmetic.
WITH t AS (
SELECT DATE '2024-02-10' AS d1,
DATE '2024-01-01' AS d2
FROM dual
)
SELECT MONTHS_BETWEEN(d1, d2) AS builtin,
(EXTRACT(YEAR FROM d1) - EXTRACT(YEAR FROM d2)) * 12
+ EXTRACT(MONTH FROM d1) - EXTRACT(MONTH FROM d2)
+ (EXTRACT(DAY FROM d1) - EXTRACT(DAY FROM d2)) / 31 AS by_hand
FROM t;Both columns show 1.2903225806, which is 1 + 9/31. This hand-written formula ignores the time of day and the month-end shortcut, so use it to understand the function, not to replace it.
Reading fractions
A fractional result is not a count of days. It is a month count whose fraction is days / 31. To get a rough day count from the fraction, multiply by 31. For exact days, subtract the dates directly, because date subtraction returns days.
The next query shows three non-aligned pairs.
SELECT MONTHS_BETWEEN(DATE '2024-03-30', DATE '2024-02-29') AS a,
MONTHS_BETWEEN(DATE '2024-03-01', DATE '2024-01-31') AS b,
MONTHS_BETWEEN(DATE '2024-04-30', DATE '2024-03-31') AS c
FROM dual;ais 1.0322580645. March 30 is not a month end, so the pair is not aligned: 1 month plus (30 - 29)/31.bis also 1.0322580645. The month difference is 2 and the day difference is 1 - 31 = -30, so the result is 2 - 30/31. This is only about one month and one day even though the dates sit two calendar months apart.cis 1. April 30 and March 31 are both last days of their months, so the month-end rule applies.
The time of day
For aligned pairs, the time of day is ignored. For non-aligned pairs, it adds to the fraction. A time difference is converted to a fraction of a day, and that fraction is divided by 31.
SELECT MONTHS_BETWEEN(
TO_DATE('2024-02-10 18:00', 'YYYY-MM-DD HH24:MI'),
TO_DATE('2024-01-10 06:00', 'YYYY-MM-DD HH24:MI')) AS same_day_of_month,
MONTHS_BETWEEN(
TO_DATE('2024-02-11 12:00', 'YYYY-MM-DD HH24:MI'),
TO_DATE('2024-01-10 00:00', 'YYYY-MM-DD HH24:MI')) AS not_aligned
FROM dual;The first column is exactly 1, because the day of the month matches and the 12-hour gap is discarded. The second is 1.0483870968, which is 1 + 1.5/31 (one day plus half a day).
If you want results that do not depend on the time of day, wrap both arguments in TRUNC. This is usually what you want for birth dates, hire dates, and contract dates.
SELECT MONTHS_BETWEEN(TRUNC(SYSDATE), TRUNC(hire_date)) AS months_of_service
FROM employees;MONTHS_BETWEEN is not the inverse of ADD_MONTHS
You might expect MONTHS_BETWEEN(ADD_MONTHS(d, n), d) to return n for any d. It does when d falls on a day that exists in every month, or is a month end. It does not always.
SELECT ADD_MONTHS(DATE '2024-01-30', 1) AS shifted,
MONTHS_BETWEEN(ADD_MONTHS(DATE '2024-01-30', 1),
DATE '2024-01-30') AS measured
FROM dual;shifted is 2024-02-29, because ADD_MONTHS clamps to the last day of February. measured is 0.9677419355, which is 1 - 1/31. February 29 is a month end but January 30 is not, so the pair is not aligned. Round trips through the two functions can drift by a day's worth of fraction near the end of a month.
Turning the result into whole months
The raw result is rarely what a report wants. Common conversions:
| Goal | Expression |
|---|---|
| Completed months (later date first) | `TRUNC(MONTHS_BETWEEN(later, earlier))` |
| Months rounded to nearest | `ROUND(MONTHS_BETWEEN(later, earlier))` |
| Display with two decimals | `ROUND(MONTHS_BETWEEN(later, earlier), 2)` |
| Completed years | `TRUNC(MONTHS_BETWEEN(later, earlier) / 12)` |
| Months left over after whole years | `MOD(TRUNC(MONTHS_BETWEEN(later, earlier)), 12)` |
TRUNC rounds toward zero. For a negative result, TRUNC(-1.29) gives -1, while FLOOR(-1.29) gives -2. Keep the later date first so the value is positive and the two behave the same.
The years and months from the last two rows give an age calculation. A fixed as-of date makes the output repeatable.
SELECT TRUNC(MONTHS_BETWEEN(DATE '2026-11-28', DATE '1990-06-15') / 12) AS age_years,
MOD(TRUNC(MONTHS_BETWEEN(DATE '2026-11-28', DATE '1990-06-15')), 12) AS extra_months
FROM dual;The total is 437 months plus 13/31 of a month. The query returns 36 years and 5 months. Replace the literal with SYSDATE to measure against the current time.
Using the result in filters
For a threshold test, compare the fraction directly. This query finds employees with at least six full months of service.
SELECT employee_id, hire_date
FROM employees
WHERE MONTHS_BETWEEN(TRUNC(SYSDATE), TRUNC(hire_date)) >= 6;Applying a function to hire_date stops Oracle from using an ordinary index on that column. When the table is large, rewrite the condition so the column stays bare:
SELECT employee_id, hire_date
FROM employees
WHERE hire_date <= ADD_MONTHS(TRUNC(SYSDATE), -6);Both forms agree on aligned dates. They can disagree by a day near month ends, because ADD_MONTHS clamps and MONTHS_BETWEEN uses 31-day fractions. Pick one definition of "six months" and apply it everywhere in the same system.
Common errors
- Reversed arguments. The sign flips, and filters like
>= 6return nothing. - Treating the fraction as days. The fraction is days divided by 31, not by the real month length.
- Expecting an integer from non-aligned dates. Only same-day-of-month pairs and month-end pairs give integers.
- Ignoring the time of day. Two non-aligned timestamps on the same calendar date can produce different fractions. Truncate the inputs.
- Passing strings. Implicit conversion depends on
NLS_DATE_FORMAT. - Using it for day counts. Subtract the dates instead.
Exercises
Exercise
What does MONTHS_BETWEEN(DATE '2024-03-31', DATE '2024-01-30') return, and why?
Show answer
2.0322580645. March 31 is a month end but January 30 is not, so the pair is not aligned. The month difference is 2 and the day difference is 31 - 30 = 1, so the result is 2 + 1/31.
Exercise
A query uses WHERE MONTHSBETWEEN(hiredate, SYSDATE) >= 6 to find staff with six or more months of service, and returns no rows. What is wrong, and how do you fix it?
Show answer
The arguments are reversed. hiredate is earlier than SYSDATE, so the result is negative. Use MONTHSBETWEEN(SYSDATE, hire_date) >= 6, or TRUNC both arguments to remove the time of day.
Exercise
Why does MONTHSBETWEEN(ADDMONTHS(DATE '2024-01-30', 1), DATE '2024-01-30') not return 1?
Show answer
ADD_MONTHS returns 2024-02-29, the last day of February. That date is a month end but 2024-01-30 is not, so the pair is not aligned and the result is 1 - 1/31, about 0.9677.