Sub topic 9
Reading fractional month results
This chapter explains how to read the decimal part of a MONTHS_BETWEEN result and what rule produces it. It assumes you have read the previous chapters on DATE values, ADD_MONTHS, and its end-of-month behavior.
Where fractions come from
MONTHS_BETWEEN(date1, date2) returns a NUMBER. The integer part counts calendar months between the two dates. The decimal part measures the leftover days, and often the leftover time of day.
Oracle applies these rules in order:
- If both dates fall on the same day of the month, the result is a whole number.
- If both dates are the last day of their months, the result is a whole number.
- Otherwise Oracle takes the difference in months, then adds the day difference divided by 31. A month is always treated as 31 days for this purpose.
The sign follows the argument order. The result is positive when date1 is later than date2, and negative when it is earlier.
SELECT MONTHS_BETWEEN(DATE '2024-03-15', DATE '2024-01-15') AS same_day,
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 |
|---|---|---|
| same_day | 2 | Both dates are on the 15th. |
| both_month_ends | 1 | March 31 and February 29 are both last days. |
| fractional | 1.29032258... | 1 month plus (10 - 1) / 31 days. |
| negative_result | -1.29032258... | Same magnitude, reversed order. |
The 31-day rule
The divisor is 31 for every pair of dates, whatever the real month lengths are. February 1 to March 1 is exactly 1 month. January 1 to February 10 is 1 + 9/31 months, even though February has 29 days in 2024.
This gives three consequences:
- A fraction is not a share of any actual month. It is "days divided by 31".
- A fraction of 0.5 means 15.5 days, not half of whatever month you are in.
- Converting a fraction back to days means multiplying by 31, not by 30 or by the real month length.
Time of day counts
DATE values carry a time. When Oracle computes the fraction, the time difference is part of the day difference. Twelve hours is half a day, so it adds 0.5/31 months.
SELECT MONTHS_BETWEEN(
TO_DATE('2024-01-16 12:00:00', 'YYYY-MM-DD HH24:MI:SS'),
DATE '2024-01-01') AS with_noon
FROM dual;The difference is 15 days and 12 hours, which is 15.5 days. Dividing by 31 gives exactly .5.
This matters when you compare SYSDATE with a stored date. SYSDATE includes the current time, so the fraction changes during the day. If you want results that depend only on the calendar date, wrap SYSDATE in TRUNC, as in MONTHS_BETWEEN(TRUNC(SYSDATE), hire_date).
Splitting a result into months and days
You often want "1 month and 9 days" rather than 1.2903. Separate the integer part from the fraction, then multiply the fraction by 31.
WITH r AS (
SELECT DATE '2024-01-01' AS d_start,
DATE '2024-02-10' AS d_end
FROM dual
)
SELECT TRUNC(MONTHS_BETWEEN(d_end, d_start)) AS whole_months,
ROUND(MOD(MONTHS_BETWEEN(d_end, d_start), 1) * 31) AS leftover_days
FROM r;This returns 1 whole month and 9 leftover days.
ROUND is needed. 9/31 repeats forever, and Oracle stores NUMBER values with about 38 digits of precision. Multiplying the stored value by 31 can give 8.9999999... instead of 9. Rounding to whole days removes that error. If your dates carry times, round to the precision you need, for example ROUND(x * 31, 2).
Both TRUNC and MOD(..., 1) work as shown only when the result is positive. For a negative result, TRUNC rounds toward zero and MOD(-1.29, 1) returns -0.29, so both parts come out negative. If the order of the dates is uncertain, put the earlier date second, or wrap the call in ABS after you have decided what the sign means.
Edge cases
The result is not the same as the day count
Compare the two measures for a range that crosses a month end:
SELECT MONTHS_BETWEEN(DATE '2024-03-01', DATE '2024-01-31') AS months,
DATE '2024-03-01' - DATE '2024-01-31' AS days
FROM dual;The result is about 1.0323 months and 30 days. The month difference is 2, and the day difference is 1 - 31 = -30, so Oracle returns 2 - 30/31. Neither date is a month end pair, and the days of the month differ, so the 31-day rule applies. The days value is the real elapsed time, and the months value is a calendar-based approximation.
A month end matches only when both dates are month ends
February 28, 2024 is not the last day of February because 2024 is a leap year. It therefore does not trigger the month-end rule against January 31.
SELECT MONTHS_BETWEEN(DATE '2024-02-28', DATE '2024-01-31') AS leap_year,
MONTHS_BETWEEN(DATE '2023-02-28', DATE '2023-01-31') AS common_year
FROM dual;leap_yearis 1 + (28 - 31)/31, which is about 0.9032.common_yearis exactly 1, because February 28 is the last day of February in 2023.
The same calendar positions give different answers depending on the year.
Whole months with TRUNC can disagree with ADD_MONTHS
ADD_MONTHS(DATE '2023-01-30', 1) returns February 28, 2023, because it clamps to the end of the month. Yet this gives a fraction below 1:
SELECT MONTHS_BETWEEN(DATE '2023-02-28', DATE '2023-01-30') AS m,
TRUNC(MONTHS_BETWEEN(DATE '2023-02-28', DATE '2023-01-30')) AS whole
FROM dual;The result is 1 + (28 - 30)/31, about 0.9355, so whole is 0. January 30 is not a month end, so the month-end rule does not apply. ADD_MONTHS says one month has passed, and TRUNC(MONTHS_BETWEEN) says none has. If your business rule says "a month is complete when ADD_MONTHS(start, n) <= end", test that condition directly instead of relying on TRUNC.
SELECT CASE WHEN ADD_MONTHS(DATE '2023-01-30', 1) <= DATE '2023-02-28'
THEN 'one full month' ELSE 'less than a month' END AS status
FROM dual;Common mistakes
| Mistake | Effect | Fix |
|---|---|---|
| Multiplying the fraction by 30 to get days | Off by a few percent | Multiply by 31. |
| Passing the earlier date first | Negative result | Put the later date first. |
| Using `SYSDATE` without `TRUNC` | Fraction changes during the day | Use `TRUNC(SYSDATE)`. |
| Comparing `= 1` against a computed fraction | Misses values like 0.99999... | Use `ROUND`, or compare with `>=` and `<`. |
| Treating the fraction as a share of the real month | Wrong in February and 30-day months | Remember the divisor is always 31. |
| Using `TRUNC` on a negative result | Rounds toward zero, not down | Use `FLOOR` if you need rounding down. |
Choosing how to present the result
Pick the presentation that matches what the reader needs:
- For a rough duration such as "about 1.3 months",
ROUND(MONTHS_BETWEEN(...), 1)is enough. - For "completed months", prefer the
ADD_MONTHStest above when month-end clamping matters. UseTRUNC(MONTHS_BETWEEN(...))only when a one-day mismatch near month ends is acceptable. - For exact elapsed time, subtract the dates and work in days. The next chapters cover ages and tenure, and the final chapter covers
INTERVALtypes for exact year-month and day-second spans.
Exercise
What does MONTHS_BETWEEN(DATE '2024-04-20', DATE '2024-02-05') return? Give the exact fraction and a value rounded to 4 decimals.
Show answer
The month difference is 2, and the day difference is 20 - 5 = 15. The days of the month differ and neither date is a month end, so the result is 2 + 15/31 = 2.48387096..., which rounds to 2.4839.
Exercise
A query computes TRUNC(MONTHS_BETWEEN(DATE '2024-02-28', DATE '2024-01-31')) and returns 0. Explain why, and say what the answer would be for 2023 dates with the same month and day.
Show answer
In 2024 February 28 is not the last day of the month, so the month-end rule does not apply. The result is 1 + (28 - 31)/31, about 0.9032, and TRUNC returns 0. In 2023 February 28 is the last day of February and January 31 is the last day of January, so the result is exactly 1.
Exercise
Write a query that returns whole months and leftover days between DATE '2024-01-01' and DATE '2024-02-10'. Why is ROUND needed when converting the fraction to days?
Show answer
SELECT TRUNC(MONTHSBETWEEN(DATE '2024-02-10', DATE '2024-01-01')) AS wholemonths, ROUND(MOD(MONTHSBETWEEN(DATE '2024-02-10', DATE '2024-01-01'), 1) * 31) AS leftoverdays FROM dual; This returns 1 and 9. ROUND is needed because 9/31 is stored with limited precision, so multiplying by 31 can produce 8.99999... instead of 9.