TIMESTAMP DIFF UDF
Description
Snowflake does not support the addition operation between TIMESTAMP data types with the - operand. In order to replicate this functionality, we have added a custom UDF.
Custom UDF overloads
TIMESTAMP_DIFF_UDF(timestamp, timestamp)
Parameters
LEFT_TS: The first
TIMESTAMPof the operation.RIGHT_TS: The
TIMESTAMPto be added.
CREATE OR REPLACE FUNCTION TIMESTAMP_DIFF_UDF(LEFT_TS TIMESTAMP, RIGHT_TS TIMESTAMP )
RETURNS VARCHAR
LANGUAGE SQL
IMMUTABLE
COMMENT = '{"origin":"sf_sc","name":"snowconvert","version":{"major":1, "minor":0},"attributes":{"component":"udf"}}'
AS
$$
WITH RESULTS(days,hours,min,sec,millisecond,sign) AS
(
SELECT
abs(TRUNC(x/1000/3600/24)) days,
abs(TRUNC(x/1000/60 / 60)-trunc(x/1000/3600/24)*24) hours,
abs(TRUNC(MOD(x/1000,3600)/60)) min,
abs(TRUNC(MOD(x/1000,60))) sec,
abs(TRUNC(MOD(x,1000))) millisecond,
SIGN(x)
FROM (SELECT TIMESTAMPDIFF(millisecond, RIGHT_TS, LEFT_TS) x ,SIGN(TIMESTAMPDIFF(millisecond, RIGHT_TS, LEFT_TS)) sign))
SELECT
IFF(SIGN>0,'+','-') || TRIM(TO_CHAR(days,'000000000')) || ' ' || TO_CHAR(hours,'00') || ':' || TRIM(TO_CHAR(min,'00')) || ':' || TRIM(TO_CHAR(sec,'00')) || '.' || TRIM(TO_CHAR(millisecond,'00000000'))
from RESULTS
$$;Oracle
Snowflake
Known Issues
1. TIMESTAMP format may differ from Oracle
The TIMESTAMP format may differ from Oracle, please consider the TIMESTAMP_OUTPUT_FORMAT setting when working with TIMESTAMP data types.
Related EWIs
No related EWIs.
Last updated