Oracle/PLSQL: Months_Between Function

In Oracle/PLSQL, the months_between function returns the number of months between date1 and date2.

Syntax

The syntax for the months_between function is:

months_between( date1, date2 )

date1 and date2 are the dates used to calculate the number of months.

If a fractional month is calculated, the months_between function calculates the fraction based on a 31-day month.

Applies To

    Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i

Example #1

months_between (to_date (2003/01/01, yyyy/mm/dd), to_date (2003/03/14, yyyy/mm/dd) )

would return -2.41935483870968

Example #2

months_between (to_date (2003/07/01, yyyy/mm/dd), to_date (2003/03/14, yyyy/mm/dd) )

would return 3.58064516129032

Example #3

months_between (to_date (2003/07/02, yyyy/mm/dd), to_date (2003/07/02, yyyy/mm/dd) )

would return 0

Example #4

months_between (to_date (2003/08/02, yyyy/mm/dd), to_date (2003/06/02, yyyy/mm/dd) )

would return 2

经验分享 程序员 微信小程序 职场和发展