oracle daysbetween,ORA-01847: day of month must be between 1 and last day of month

Looks like some kind of query rewrite bug that only kicks in when you add the where clause. If I skip the where clause and make it into a table:SQL> alter session set nls_date_format = 'dd/mm/yyyy';Session altered.SQL> create table bob2 aswith sample_data(dt) as(select '00/02/2015' from dual union allselect '10/02/2015' from dual union allselect '02/2015' from dual union allselect '13/02/2015' from dual union allselect '29/02/2015' from dual union allselect '29/02/2016' from dual union allselect '13/06/2015' from dual union allselect '13/06/2005' from dual union allselect '01//2015' from dual union allselect null from dual)select *from(select dt   from sample_data   where dt in (select to_char(to_date('01/01/2013')+rownum-1,'dd/mm/yyyy')                      from dual                      connect by level<=2000)  );  2    3    4    5    6    7    8    9   10   11   12   13   14   15   16   17   18   19   20   21Table created.

We can then desc the table and see that oracle thinks dt is a varchar, which it should:SQL> desc bob2; Name                                      Null?    Type ----------------------------------------- -------- ---------------------------- DT                                                 VARCHAR2(10)

But if we then try and run the query with the where clause:SQL> with sample_data(dt) as(select '00/02/2015' from dual union allselect '10/02/2015' from dual union allselect '02/2015' from dual union allselect '13/02/2015' from dual union allselect '29/02/2015' from dual union allselect '29/02/2016' from dual union allselect '13/06/2015' from dual union allselect '13/06/2005' from dual union allselect '01//2015' from dual union allselect null from dual)select *from(select dt   from sample_data   where dt in (select to_char(to_date('01/01/2013')+rownum-1,'dd/mm/yyyy')                      from dual                      connect by level<=2000)  )where to_date(dt,'dd/mm/yyyy') between to_date('01/01/2000', 'dd/mm/yyyy') and to_date('01/01/2020', 'dd/mm/yyyy');  2    3    4    5    6         7    8    9   10   11   12   13   14   15   16   17   18   19   20   21(select '00/02/2015' from dual union all        *ERROR at line 2:ORA-01847: day of month must be between 1 and last day of monthSQL>

It's trying to to_Date the strings in the WITH clause. It really shouldn't.

I would have thought you could fix this with a hint, but I tried the obvious NO_QUERY_TRANSFORMATION and that didn't help. Maybe someone else has an idea.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值