问题
Hi i am getting an error as expected something like END keyword between DATEDIFF and ( for the below statement under
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,
回答1:
Assuming that your fields are DATE datatype (otherwise you'll need to cast):
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
回答2:
There's no DATEDIFF
function in Teradata.
This is a generic SQL UDF I wrote a few years ago for calculating the difference of two timestamps in seconds:
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))
;
来源:https://stackoverflow.com/questions/33780422/teradata-datediff-error