Sub topic 9
Reading Fractional Month Results
This chapter assumes you can write basic SELECT statements, use the DUAL table, and recognize Oracle DATE values. You should also know that an Oracle DATE stores both a calendar date and a time down to seconds.
MONTHS_BETWEEN measures the number of months from one date to another:
MONTHS_BETWEEN(later_date, earlier_date)The order matters. Reversing the arguments changes the sign.
Whole months and fractional months
Oracle returns a whole number when:
- Both dates have the same day of the month.
- Both dates are the last day of their respective months.
Otherwise, Oracle returns a fractional result. The fractional part uses a 31-day month as its basis.
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;The results are approximately:
| Expression | Result | Reason |
|---|---|---|
| March 15 minus January 15 | 2 | Same day of month |
| March 31 minus February 29 | 1 | Both dates are month ends |
| February 10 minus January 1 | 1.2903 | One month plus 9/31 |
| January 1 minus February 10 | -1.2903 | The arguments are reversed |
The result 1.2903 does not mean “one month and exactly 0.2903 of February.” It means one whole month plus nine days represented as 9 / 31.
How the 31-day rule works
Consider these dates:
SELECT MONTHS_BETWEEN(DATE '2024-03-20', DATE '2024-01-11') AS month_difference
FROM dual;The dates are one month and nine days apart when their day numbers are compared:
- January 11 to February 11 is one month.
- February 11 to February 20 is nine days.
- Oracle represents those nine days as
9 / 31.
The approximate result is:
1 + 9 / 31 = 1.29032258This rule makes fractional results useful for calculations, but the result is not a precise duration in days. A calendar month can contain 28, 29, 30, or 31 days.
A time component can also affect a fractional result when the day numbers differ:
SELECT MONTHS_BETWEEN(
TO_DATE('2024-03-10 18:00:00', 'YYYY-MM-DD HH24:MI:SS'),
TO_DATE('2024-01-01 06:00:00', 'YYYY-MM-DD HH24:MI:SS')
) AS month_difference
FROM dual;The twelve-hour time difference contributes to the fractional portion. When the day numbers match, Oracle's whole-month rule takes precedence, so time differences may not produce a fractional result in the way you expect. Test boundary cases instead of assuming that every elapsed period is converted to a simple month count.
Rounding fractional results
MONTHS_BETWEEN returns a number. You can use numeric functions to present it at a useful precision:
SELECT MONTHS_BETWEEN(DATE '2024-02-10', DATE '2024-01-01') AS raw_value,
ROUND(MONTHS_BETWEEN(DATE '2024-02-10', DATE '2024-01-01'), 2) AS rounded_value,
TRUNC(MONTHS_BETWEEN(DATE '2024-02-10', DATE '2024-01-01')) AS whole_months
FROM dual;ROUND changes how the number is displayed or used. TRUNC removes the fractional part without rounding. For a positive result of 1.2903, TRUNC returns 1. For a negative result, TRUNC moves toward zero, so TRUNC(-1.2903) returns -1.
Do not use TRUNC(MONTHS_BETWEEN(...)) as a general age calculation. Someone can be 1 month and 30 days old but still have a truncated result of 1. Age and tenure often require a separate rule for completed months or completed years.
Exercise
What does MONTHS_BETWEEN(DATE '2024-04-10', DATE '2024-02-01') return conceptually, and why?
Show answer
It returns 2 + 9/31, approximately 2.2903. The dates are two whole calendar months apart by month number, plus nine day numbers, and the remainder uses a 31-day basis.