SQL MONTHS_BETWEEN Function
SQL MONTHS_BETWEEN calculates the number of months between two dates. SQL MONTHS_BETWEEN Function is simple subtraction of months between two date values. The input dates can be in either order, but if the second date is later, the result will be negative. The result will be an integer number of months for any case in which both dates correspond to the same day of the month, or for any case in which both dates correspond to the last day of their respective months. Otherwise, Oracle calculates a fractional result based on a 31-day month, also considering any time-of-day components of the input dates.
The syntax is
MONTHS_BETWEEN(later_date , earlier_date)
The result corresponds to later_date – earlier_date.
Example 1 :
The following SQL MONTHS_BETWEEN Function will return ‘11’ as a result value which is exact number of months between two input dates:
SELECT MONTHS_BETWEEN (’25-NOV-2013’,’25-DEC-2012’) FROM dual;
Output:
Example 2:
The following SQL MONTHS_BETWEEN Function will return ‘-13.1612903225806’ because the dates do not correspond to same day of month and result value is negative because dates are switched.
SELECT MONTHS_BETWEEN ('25-NOV-2012','30-DEC-2013') FROM dual;
Output: