Re ORA-01843:不是有效月份错误

时间:2017-06-27 15:36:36

标签: oracle date plsql oracle11g oracle-sqldeveloper

查询:

SELECT t3.e_id, t6.ei_id
FROM t3, t6
WHERE 
(TO_CHAR(t6.EI_Q17, 'YYYYMMDD') BETWEEN 20160801 AND 20170731)
AND (t3.E_STATUS                = 'W')
AND (t3.E_START                 < '1/8/2016')
AND (t3.E_END                   > '31/7/2017')

但我收到错误:ORA-01843: not a valid month.

知道如何解决这个问题吗?

谢谢, 的Ar

2 个答案:

答案 0 :(得分:2)

使用标准日期语法和显式join s:

SELECT t3.e_id, t6.ei_id
FROM t3 CROSS JOIN t6
WHERE t6.EI_Q17 BETWEEN DATE '2016-08-01' AND DATE '2017-07-31' AND
      t3.E_STATUS = 'W' AND
      t3.E_START < DATE '2016-08-01' AND
      t3.E_END > DATE '2017-07-31';

如果您使用DATE和ISO 8601标准日期格式,则无需担心日期常量的本地化设置。您也不需要使用函数调用来混乱查询。

答案 1 :(得分:1)

首先,您必须遵循Gordon建议使用标准日期语法和显式连接。其次,根据您发布的查询,t6.EI_Q17varchardate SELECT t3.e_id, t6.ei_id FROM t3 CROSS JOIN t6 WHERE TO_CHAR (TO_DATE (t6.EI_Q17, 'YYYY-MM-DD'), 'YYYYMMDD') BETWEEN 20160801 AND 20170731 AND t3.E_STATUS = 'W' AND t3.E_START < TO_DATE ('2016-08-01', 'YYYY-MM-DD') AND t3.E_END > TO_DATE ('2017-07-31', 'YYYY-MM-DD') 。在这种情况下,你需要像下面这样投两次:

SQL> select dt 
    from
    (select to_char(to_date('2017-06-01','YYYY-MM-DD'),'YYYYMMDD') dt from dual )
    where dt between 20170601 and 20170603 ;

简单演示:

20170601

输出:

{{1}}