求两个日期间的月数差

项目中用的,小记一下

select 
    abs(MONTHS_BETWEEN(to_date('201001', 'yyyymm'),to_date('201002', 'yyyymm'))) + 1
from dual;

return 
2


In Oracle/PLSQL, the abs function returns the absolute value of a number.

The syntax for the abs function is:

abs( number )

number is the number to convert to an absolute value.

 

Applies To:

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

 

For example:

abs(-23) would return 23
abs(-23.6) would return 23.6
abs(-23.65) would return 23.65
abs(23.65) would return 23.65
abs(23.65 * -1)

would return 23.65

 

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

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 8i, Oracle 9i, Oracle 10g, Oracle 11g

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

你可能感兴趣的:(oracle)