MONTHS_BETWEEN 获取两个日期之间的月份数

Syntax

Description of months_between.gif follows

Purpose

MONTHS_BETWEEN returns number of months between dates date1 and date2. The month and the last day of the month are defined by the parameter NLS_CALENDAR. 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.

 

SQL> select months_between(add_months(sysdate, 5), sysdate) as "months" from dual
  2  /

    months
----------
         5

SQL> select months_between(sysdate, add_months(sysdate, 9)) as "months" from dual
  2  /

    months
----------
        -9

 

SQL> select months_between(sysdate+20, sysdate) as "months" from dual
  2  /

    months
----------
 .64516129

SQL> select months_between(sysdate, sysdate+31) as "months" from dual
  2  /

    months
----------
-1.0322581

SQL> select months_between(sysdate+30, sysdate) as "months" from dual
  2  /

    months
----------
         1

 

posted @ 2012-08-31 18:20  Dragon Luo  阅读(5870)  评论(0编辑  收藏  举报