select months_between(to_date('2011-4-28','yyyy-MM-dd'),to_date('2011-2-28','yyyy-MM-dd'))
from dual;
MONTHS_BETWEEN(TO_DATE('2011-4
------------------------------
2
select months_between(to_date('2011-4-29','yyyy-MM-dd'),to_date('2011-2-28','yyyy-MM-dd'))
from dual;
MONTHS_BETWEEN(TO_DATE('2011-4
------------------------------
2.03225806451613
select months_between(to_date('2011-4-30','yyyy-MM-dd'),to_date('2011-2-28','yyyy-MM-dd'))
from dual;
MONTHS_BETWEEN(TO_DATE('2011-4
------------------------------
2
2011-4-30日期大于2011-4-29,按理说第三个结果应该大于第二个结果,但是结果却是相反的。
后来查阅了oracle的官方文档,是这样解释的:(小数部分是按照一个月31天计算的)
MONTHS_BETWEEN returns number of months between dates date1 and date2. 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.
select 1/31 from dual;
1/31
----------
0.03225806
from dual;
MONTHS_BETWEEN(TO_DATE('2011-4
------------------------------
2
select months_between(to_date('2011-4-29','yyyy-MM-dd'),to_date('2011-2-28','yyyy-MM-dd'))
from dual;
MONTHS_BETWEEN(TO_DATE('2011-4
------------------------------
2.03225806451613
select months_between(to_date('2011-4-30','yyyy-MM-dd'),to_date('2011-2-28','yyyy-MM-dd'))
from dual;
MONTHS_BETWEEN(TO_DATE('2011-4
------------------------------
2
2011-4-30日期大于2011-4-29,按理说第三个结果应该大于第二个结果,但是结果却是相反的。
后来查阅了oracle的官方文档,是这样解释的:(小数部分是按照一个月31天计算的)
MONTHS_BETWEEN returns number of months between dates date1 and date2. 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.
select 1/31 from dual;
1/31
----------
0.03225806
这样就能解释了。