问题:某行包含脏数据,无法将其转换为日期,因此查询失败
在将数据传输到sql服务器时,我们正在转换为日期时间
查询要转换为日期时间
select TIMESTAMP('1970-01-01', '00:00:00') +(Startdate/1000) SECONDS from tablename
需要查询以查找错误数据
所需查询:
select TIMESTAMP('1970-01-01', '00:00:00') +(Startdate/1000) SECONDS
from tablename
where iserror (TIMESTAMP('1970-01-01', '00:00:00') +(Startdate/1000) SECONDS) = 1
答案 0 :(得分:0)
您可以创建标量函数来抑制此类错误:
--#SET TERMINATOR @
create or replace function ms2ts(p_milliseconds bigint)
returns timestamp
contains sql
deterministic
no external action
begin
declare continue handler for sqlexception begin end;
return TIMESTAMP('1970-01-01', '00:00:00') +(p_milliseconds/1000) SECONDS;
end@
-- Usage:
select *
from
(
select ms2ts(startdate) ts, Startdate
from table(values
power(bigint(2), 45)
, power(bigint(2), 60)
) tablename (Startdate)
)
-- where ts is null
@
TS STARTDATE
--------------------- ----------------------
3084-12-12 12:41:28.0 35184372088832
<null> 1152921504606846976