【问题标题】:DB2 seconds to time conversion issueDB2 秒到时间转换问题
【发布时间】:2019-06-14 05:28:11
【问题描述】:

问题:某行有脏数据,无法转换为日期,所以查询失败

  • 在 db2 中,日期以秒为单位存储
  • 在将数据传输到 sql server 时,我们将转换为日期时间

    查询转换为日期时间

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

【问题讨论】:

  • 有什么问题?
  • 某行有脏数据无法转换为日期,所以查询失败
  • @vignesh 请不要让我们猜测。如果您的查询失败 - 您得到的确切错误代码和消息是什么? “脏数据”是什么意思? Startdate 列的数据类型是什么?是[var]char,是否包含一些不可转换为int 的数据?
  • 对不起。返回错误消息:日期时间算术运算或日期时间标量函数的结果不在有效日期范围内

标签: date type-conversion db2 seconds


【解决方案1】:

您可以创建一个标量函数来抑制此类错误:

--#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   

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多