Sub topic 8
Measuring Distance with `MONTHS_BETWEEN`
This chapter assumes you can write basic SELECT queries, use WHERE, and work with the DUAL table. It also assumes you know that an Oracle DATE includes both a calendar date and a time of day.
MONTHS_BETWEEN returns the number of months between two dates:
MONTHS_BETWEEN(later_date, earlier_date)The order matters. Reversing the arguments changes the sign.
Whole-month results
When both dates have the same day of the month, Oracle returns a whole number. Oracle also returns a whole number when both dates are the last day of their months.
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-01-15', DATE '2024-03-15') AS reversed
FROM dual;The results are:
| Expression | Result |
|---|---|
| March 15 minus January 15 | 2 |
| March 31 minus February 29 | 1 |
| January 15 minus March 15 | -2 |
A negative result means the first date is earlier than the second date.
Fractional months
When the day numbers differ, Oracle calculates a fractional month. The fractional part uses a 31-day month, even when the dates are in shorter months.
SELECT MONTHS_BETWEEN(
DATE '2024-02-10',
DATE '2024-01-01'
) AS month_distance
FROM dual;There is one complete month from January 1 to February 1, followed by nine days. The result is therefore:
1 + 9/31 = 1.29032258...The result is a NUMBER, so you can round or truncate it for reporting:
SELECT ROUND(
MONTHS_BETWEEN(DATE '2024-02-10', DATE '2024-01-01'),
2
) AS rounded_months,
TRUNC(
MONTHS_BETWEEN(DATE '2024-02-10', DATE '2024-01-01')
) AS complete_months
FROM dual;Do not interpret a fractional month as an exact number of days divided by one fixed month length. It follows Oracle's calendar rules and the 31-day fractional-month rule.
Time-of-day effects
The time component can affect a fractional result when the dates do not have matching day numbers. If you want to measure whole calendar months based only on dates, remove the time first:
SELECT MONTHS_BETWEEN(
TRUNC(end_date),
TRUNC(start_date)
) AS calendar_months
FROM subscriptions;This does not make every result an integer. It only prevents hours, minutes, and seconds from changing the calculation.
The following flow shows the main decision:
Age and tenure calculations
For completed years and months, first calculate whole months, then split that number into groups of 12:
SELECT TRUNC(
MONTHS_BETWEEN(SYSDATE, DATE '1990-06-15') / 12
) AS completed_years,
MOD(
TRUNC(MONTHS_BETWEEN(SYSDATE, DATE '1990-06-15')),
12
) AS extra_months
FROM dual;This calculation reports completed calendar months and years. It is different from calculating an approximate age as a number of days divided by 365.25. For business rules, confirm how leap days and birthdays should be handled.
Exercise
What does MONTHS_BETWEEN(DATE '2024-03-31', DATE '2024-02-29') return, and why?
Show answer
It returns 1 because both dates are the last day of their respective months.