我刚刚查询了SQL Server中两个日期(当月除外)之间的月数:
SELECT
(DATEDIFF(MONTH, DATEADD(MONTH, 1, '2016-02-20 00:00:00.000') -
DAY(DATEADD(MONTH, 1, '2016-02-20 00:00:00.000')), '2016-12-31 00:00:00.000'))
以上将返回10.我们如何在Oracle中实现这一目标?
答案 0 :(得分:0)
我想 - 至少10g及以上 - 正确的功能是
SELECT MONTHS_BETWEEN([date1],[date2]) FROM dual
此外,可以计算两个日期之间的差异
SELECT [date2] - [date1] FROM dual
这会在几天内给你带来差异。
答案 1 :(得分:0)
有几种不同的方法来计算月份差异; 一个好的起点可能是months_between:
SQL> with testDates (date1, date2) as
2 (
3 select date '2016-12-31', date '2016-02-20' from dual union all
4 select date '2016-02-01', date '2016-01-31' from dual union all
5 select date '2016-12-30', date '2016-11-30' from dual union all
6 select date '2016-12-29', date '2016-11-30' from dual
7 )
8 select date1, date2, months_between(date1, date2) diff, floor(months_between(date1, date2) ) floor_diff
9 from testDates;
DATE1 DATE2 DIFF FLOOR_DIFF
--------- --------- ---------- ----------
31-DIC-16 20-FEB-16 10,3548387 10
01-FEB-16 31-GEN-16 ,032258065 0
30-DIC-16 30-NOV-16 1 1
29-DIC-16 30-NOV-16 ,967741935 0
答案 2 :(得分:0)
在Oracle11g中查找两个日期之间的月份总和的正确查询。请尝试
SELECT COUNT(0)TOTAL_MONTHS FROM(选择日期' 2015-01-05' START_DATE, 日期' 2015-12-15'结束日期 从双方) 连接水平< = MONTHS_BETWEEN( TRUNC(END_DATE,' MM&#39), TRUNC(START_DATE,' MM')) + 1