简体   繁体   English

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

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

Oracle 11g Oracle 11g

here is a quick one hopefully. 希望这是一个快速的。

Below is part of a script that gets date only from from the next month first day of next month to last day. 以下是脚本的一部分,该脚本仅从下个月的下个月第一天到最后一天获取日期。 But today 29th feb it thrown an error of ORA-01839: date not valid for month specified 但是今天29日它发出错误的ORA-01839:日期对指定月份无效

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

Is there a way round this. 有没有办法解决这个问题。 Many thanks 非常感谢

I have seen this as well and I consider this a bug in Oracle. 我也看到了这一点,我认为这是Oracle中的一个错误。

The workaround is to use add_months() instead : 解决方法是使用add_months()代替:

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

I would probably use add_months() as a_horse_with_no_name suggests, but just as an alternative if you want to use intervals, you can move the point you do the truncation in the first expression, and include the same truncation in the second expression: 我可能会使用add_months()作为a_horse_with_no_name建议,但是如果你想使用间隔,你可以移动你在第一个表达式中截断的点,并在第二个表达式中包含相同的截断:

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 

This works because all months have a first day, so you don't trip over the documented caveat . 这是有效的,因为所有月份都有第一天,因此您不会因为记录在案的警告而绊倒。

When interval calculations return a datetime value, the result must be an actual datetime value or the database returns an error 当间隔计算返回datetime值时,结果必须是实际的datetime值,否则数据库将返回错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM