ORA-01830:日期格式图片在转换整个输入字符串01830之前结束

时间:2018-11-22 16:44:04

标签: sql oracle

我的查询:

SELECT COUNT(*)
FROM pd_heat_data
WHERE treatend_act LIKE TO_CHAR(current_date + 1,'YYYY-MM-DD%') AND (
     ( TO_CHAR(TO_DATE(treatend_act,'YYYY-MM-DD HH24:MI:SS'),'HH24:MI:SS') 
> '00:00:00' ) 
AND ( TO_CHAR(TO_DATE(treatend_act,'YYYY-MM-DD HH24:MI:SS'),'HH24:MI:SS') 
< '06:00:00' )
);

表结构:

HeatID         TREATEND_ACT(varchar2)
0001027184     2018-11-23 02:20:25,906
0001027399     2018-11-23 04:45:02,571

系统日期和时间设置:

Short Date: yyyy-MM-dd
Long Date:  dd MMMMM,yyyy
Short Time: hh:mm tt
Long Time:  hh:mm:ss tt
No AM andPM

1 个答案:

答案 0 :(得分:1)

由于您的查询尝试将整个字符串转换为to_date函数整体无法识别的日期格式,因此您可以尝试首先将substr应用于:

with pd_heat_data( treatend_act ) as
( select '2018-11-23 02:20:25,906' from dual )
select TO_CHAR(
          TO_DATE(substr(treatend_act,1,19),'YYYY-MM-DD HH24:MI:SS')
       ,'HH24:MI:SS') as "Derived Time"
  from pd_heat_data;

Derived Time
------------
  02:20:25

以及确切的查询:

SELECT COUNT(*)
  FROM pd_heat_data
 WHERE treatend_act LIKE TO_CHAR(current_date + 1,'YYYY-MM-DD%') AND (
     ( TO_CHAR(TO_DATE(substr(treatend_act,1,19),'YYYY-MM-DD HH24:MI:SS'),'HH24:MI:SS') 
> '00:00:00' ) 
AND ( TO_CHAR(TO_DATE(substr(treatend_act,1,19),'YYYY-MM-DD HH24:MI:SS'),'HH24:MI:SS') 
< '06:00:00' )
);

或者不需要额外的转换,substr可以直接应用为@Hilarion(感谢他)指出:

SELECT COUNT(*)
  FROM pd_heat_data
 WHERE treatend_act LIKE TO_CHAR(current_date + 1,'YYYY-MM-DD%') 
   AND substr(treatend_act,1,19) > '2018-11-23 00:00:00'  
   AND substr(treatend_act,1,19) < '2018-11-23 06:00:00';

,并尽可能尝试将treatend_act处的数据移动到date类型的列。