当我试图计算两个日期之间的月份时,我得到了错误的结果。
SELECT ROUND(MONTHS_BETWEEN(TO_DATE('23/01/2015','dd/mm/yyyy'),
TO_DATE('26/10/2014','dd/mm/yyyy')))
FROM dual;
SELECT ROUND(MONTHS_BETWEEN(TO_DATE('25/05/2015','dd/mm/yyyy'),
TO_DATE('05/05/2015','dd/mm/yyyy')))
FROM dual;
我必须得到第一个代码 - 4
在第二 - 1
但是我没那么做。
答案 0 :(得分:0)
试试这个
select
trunc( months_between(TO_DATE('2015/01/23','yyyy/mm/dd'),
TO_DATE('2014/10/26','yyyy/mm/dd')) ) mths
FROM dual;
答案 1 :(得分:0)
试试这个:
SELECT round(MONTHS_BETWEEN(TO_DATE('26/10/2014','dd/mm/yyyy'),TO_DATE('23/01/2015','dd/mm/yyyy')
))
FROM dual;
-3
10/2014 to 11/2014 -- 1
11/2014 to 12/2015 -- 1
12/2015 to 01/2015 -- 1
so its 3.
SELECT ROUND(MONTHS_BETWEEN(TO_DATE('05/05/2015','dd/mm/yyyy'),TO_DATE('25/05/2015','dd/mm/yyyy')
))
FROM dual;
-1
答案 2 :(得分:0)
试试这个:
select
ceil(months_between(add_months(
trunc(to_date('23/01/2015','dd/mm/yyyy'),'MM'),1)-1, --last day of month
trunc(to_date('26/10/2014','dd/mm/yyyy'),'MM')) --first_day of month
) months_between
from dual;
select
ceil(months_between(add_months(
trunc(to_date('25/05/2015','dd/mm/yyyy'),'MM'),1)-1, --last day of month
trunc(to_date('05/05/2015','dd/mm/yyyy'),'MM'))--first_day of month
) months_between
from dual;
它返回4和1。 SQL Fiddle
答案 3 :(得分:0)
最终我明白了:
SELECT TRUNC(ROUND(MONTHS_BETWEEN(TRUNC(TO_DATE('23/01/2015','dd/mm/yyyy'),'mm'),TRUNC(TO_DATE('15/01/2015','dd/mm/yyyy'),'mm')))) + 1 FROM dual ;
答案 4 :(得分:0)
SELECT
MONTHS_BETWEEN(LAST_DAY(DATE '2015-01-23')+1, -- 1st of next month
TRUNC(DATE'2014-10-26', 'mon')) -- 1st of month
,MONTHS_BETWEEN(LAST_DAY(DATE '2015-05-25')+1,
TRUNC(DATE'2015-05-05', 'mon'))
FROM DUAL;
请参阅Fiddle