简体   繁体   English

Oracle错误:ORA-01839:日期对指定的月份无效

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

OK guys, first of all, I have checked many websites about this error and, unfortunately, none of them helped me. 好的,首先,我已经检查了许多关于此错误的网站,不幸的是,他们都没有帮助我。 I have the simple following query: 我有简单的以下查询:

select * from (   
       select to_date(cal.year || cal.month || cal.day, 'yyyymmdd') as datew, 
              cal.daytype as type
       from vw_calendar cal)
where datew > sysdate;

When I try to execute the entire query, this error shows up: 当我尝试执行整个查询时,会出现此错误:

ORA-01839: date not valid for month specified ORA-01839:日期对指定的月份无效

If I execute only the query: 如果我只执行查询:

select to_date(cal.year || cal.month || cal.day, 'yyyymmdd') as datew, 
       cal.daytype as type
from vw_calendar cal;

It worked absolutely fine. 它工作得非常好。 If you want to view the results of the query: http://pastebin.com/PV95g3ac 如果要查看查询结果: http//pastebin.com/PV95g3ac

I checked the days of the month like day 31 or leap year and everything seems correct. 我检查了当月的日子,如第31天或闰年,一切似乎都正确。 I don't know what to do anymore. 我不知道该怎么办了。 My database is a Oracle 10g and I tried to execute the query on SQL Developer and PL/SQL Developer. 我的数据库是Oracle 10g,我试图在SQL Developer和PL / SQL Developer上执行查询。 Same error on both IDE. 两个IDE都出现相同的错误。

Actually the issue is some months have 30 days and some have 31, so the query which you are forming, it is trying to get 31st day of that month which is invalid and hence the error. 实际上问题是几个月有30天,有些有31,所以你正在形成的查询,它试图得到那个月的第31天,这是无效的,因此错误。 For Example: 例如:

it may be trying for date like: 31-NOV-2016 which is not correct, NOV has only 30 days. 它可能正在尝试日期如:31-NOV-2016这是不正确的,NOV只有30天。

Well, I found a workaround , not a solution. 好吧,我找到了解决方法 ,而不是解决方案。 If anyone knows the "correct" solution for this problem, I appreciate if you share with us. 如果有人知道这个问题的“正确”解决方案,我感谢您与我们分享。 My "solution" convert the date to number and then compare with a sysdate (converted too). 我的“解决方案”将日期转换为数字,然后与sysdate (也转换)进行比较。 Take a look: 看一看:

select * from 
       ( select to_number(cal.year||cal.month||cal.day) as datew, 
                cal.daytype as type from vw_calendar cal ) a 
where a.datew > to_number(to_char(sysdate, 'yyyymmdd'));

Thanks to everyone! 谢谢大家!

Use alias name for the inner query 为内部查询使用别名

select * from (   
       select to_date(cal.year || cal.month || cal.day, 'yyyymmdd') as datew, 
              cal.daytype as type
       from vw_calendar cal) a
where a.datew > sysdate;

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

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