Oracle ORA-01839: 日期对于指定的闰年无效

Oracle ORA-01839: date not valid for month specified Leap Year

甲骨文 11g

希望这是一个快速的。

以下是仅从下个月获取日期的脚本的一部分 下个月的第一天到最后一天。但是今天 2 月 29 日,它抛出了一个错误 ORA-01839: 指定月份的日期无效

M.MS_DATE between trunc(sysdate + interval '1' month,'MM') and last_day(sysdate + interval '1' month)

有没有办法解决这个问题。非常感谢

我也看到了这个,我认为这是 Oracle 中的一个错误。

解决方法是使用 add_months() 代替:

between trunc(add_months(sysdate,1),'MM') and last_day(add_months(sysdate,1));

我可能会按照 a_horse_with_no_name 的建议使用 add_months(),但作为替代方案,如果您想使用间隔,您可以移动第一个表达式中进行截断的点,并包括第二个表达式中的相同截断:

select trunc(sysdate, 'MM') + interval '1' month as first_day,
  last_day(trunc(sysdate, 'MM') + interval '1' month) as last_day
from dual;

FIRST_DAY  LAST_DAY  
---------- ----------
2015-02-01 2015-02-28 

之所以可行,是因为所有月份都有第一天,因此您不会被 documented caveat 绊倒。

When interval calculations return a datetime value, the result must be an actual datetime value or the database returns an error