ORA-01847 月份中的某一天必须介于 1 和月份最后一天之间 - 但数据正常

Jog*_*ggl 5 sql oracle date

问题已解决 - 请参阅本文末尾。

当我在 select 子句中调用 to_date 时,一切正常 - 得到包含 12 条记录的结果集:

select value1,to_date(value1,'DD.MM.YYYY') 
  from variableindex 
  where 
    value1 is not null 
    and value1 <> '0' 
    and creation_time_ > to_timestamp('20140307','YYYYMMDD')
  order by 2
Run Code Online (Sandbox Code Playgroud)

回报

'VALUE1'     'TO_DATE(VALUE1,'DD.MM.YYYY')'
'25.11.2013' 25.11.13
'12.03.2014' 12.03.14
'12.03.2014' 12.03.14
'12.03.2014' 12.03.14
'12.03.2014' 12.03.14
'12.03.2014' 12.03.14
'14.03.2014' 14.03.14
'14.03.2014' 14.03.14
'14.03.2014' 14.03.14
'14.03.2014' 14.03.14
'20.03.2014' 20.03.14
'20.03.2014' 20.03.14
Run Code Online (Sandbox Code Playgroud)

每个日期字符串都已按预期进行转换。

如果我将以下行添加到 where 子句

and to_date(value1,'DD.MM.YYYY') < to_date('20140301','YYYYMMDD')
Run Code Online (Sandbox Code Playgroud)

我会收到:

ORA-01847: Tag des Monats muss zwischen 1 und letztem Tag des Monats liegen
01847. 00000 -  "day of month must be between 1 and last day of month"
*Cause:    
*Action:
Run Code Online (Sandbox Code Playgroud)

不,这真的很糟糕...我将查询更改为

where id_ in (...)
Run Code Online (Sandbox Code Playgroud)

并使用与原始查询中相同的 12 个记录集 ID。没有错误...

非常感谢@GordonLinoff - 这就是我现在使用查询的方式:

select value1,to_date(value1,'DD.MM.YYYY') from variableindex 
   where 
   (case when value1 <> '0'  then to_date(value1,'DD.MM.YYYY') end) >  to_timestamp('20131114','YYYYMMDD')
   and creation_time_ > to_timestamp('20140307','YYYYMMDD')
order by 2;
Run Code Online (Sandbox Code Playgroud)

Gor*_*off 5

这是您使用以下where子句的查询:

select value1, to_date(value1,'DD.MM.YYYY') 
from variableindex 
where value1 is not null and
      value1 <> '0' and
      creation_time_ > to_timestamp('20140307', 'YYYYMMDD') and
      to_date(value1 'DD.MM.YYYY') < to_date('20140301', 'YYYYMMDD')
order by 2;
Run Code Online (Sandbox Code Playgroud)

Oracle 不保证where. 因此,value <> '0'不保证在最后一个条件之前运行。这恰好是 SQL Server 上的一个大问题。一种解决方案是使用case语句:

select value1,to_date(value1, 'DD.MM.YYYY') 
from variableindex 
where value1 is not null and
      value1 <> '0' and
      creation_time_ > to_timestamp('20140307', 'YYYYMMDD') and
      (case when value <> '0' then to_date(value1, 'DD.MM.YYYY') end) <
          to_date('20140301', 'YYYYMMDD')
order by 2;
Run Code Online (Sandbox Code Playgroud)

相当丑陋,但它可能会解决你的问题。