Teradata DATEDIFF错误

时间:2015-11-18 12:42:52

标签: teradata datediff

嗨我正在收到DATEDIFF之间的END关键字之类的错误和( 对于

下的声明
select
   case when CC.CASE_STS_CD in ( 'Closed', 'Auto Closed') then
        DATEDIFF(second,CC.REC_DTTM_PST,CC.CRT_DTTM_PST) end as CASE_RES_DUR_IN_SECS,

2 个答案:

答案 0 :(得分:0)

假设您的字段是DATE数据类型(否则您需要转换):

SELECT
CASE WHEN
    CC.CASE_STS_CD IN ('Closed','Auto Closed') THEN
    (CC.REC_DTTM_PST - CC.CRT_DTTM_PST) * 86400
END AS CASE_RES_DUR_IN_SECS

答案 1 :(得分:0)

Teradata中没有DATEDIFF功能。

这是我几年前写的用于计算两个时间戳差异的通用SQL UDF:

REPLACE FUNCTION TimeStamp_Diff_Seconds
(
   ts1 TIMESTAMP(6)
  ,ts2 TIMESTAMP(6)
)
RETURNS DECIMAL(18,6)
LANGUAGE SQL
CONTAINS SQL
RETURNS NULL ON NULL INPUT
DETERMINISTIC
SQL SECURITY DEFINER
COLLATION INVOKER
INLINE TYPE 1
RETURN
(CAST((CAST(ts2 AS DATE)- CAST(ts1 AS DATE)) AS DECIMAL(18,6)) * 60*60*24)
      + ((EXTRACT(  HOUR FROM ts2) - EXTRACT(  HOUR FROM ts1)) * 60*60)
      + ((EXTRACT(MINUTE FROM ts2) - EXTRACT(MINUTE FROM ts1)) * 60)
      +  (EXTRACT(SECOND FROM ts2) - EXTRACT(SECOND FROM ts1))
;