w3resource

Oracle MONTHS_BETWEEN function

How to calculate the number of months between two dates in Oracle?

The MONTHS_BETWEEN() function is used to get the number of months between dates (date1, date2). See the following conditions:

  • If date1 is later than date2, then the result is positive.
  • If date1 is earlier than date2, then the result is negative.
  • If date1 and date2 are either the same days of the month or both last days of months, then the result is always an integer.
  • Otherwise, Oracle Database calculates the fractional portion of the result based on a 31-day month and considers the difference in time components date1 and date2.

Syntax:

MONTHS_BETWEEN(date1, date2)     

Parameters:

Name Description
date1 The first date
date2 The second date.

Applies to:
Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i

Pictorial Presentation

Pictorial Presentation of Oracle MONTHS_BETWEEN function

Example: Oracle MONTHS_BETWEEN () function

The following statement calculates the months between two specified dates:

SQL> SELECT MONTHS_BETWEEN
  2     (TO_DATE('02-02-2015','MM-DD-YYYY'),
  3     TO_DATE('12-01-2014','MM-DD-YYYY') ) "Months"
  4     FROM DUAL;.

Sample Output:

    Months
----------
2.03225806

Previous: LOCALTIMESTAMP
Next: NEW_TIME



Share this Tutorial / Exercise on : Facebook and Twitter