How to convert epoch time to Human readable in Teradata -
in teradata table, have epoch timestamps under column dhtimestamp
dhtimestamp 1435308067705 1434965874565 1434763800794 1434775876034 1434765207057
how can convert epoch timestamp human date/time format on teradata?
this sql udf standard unixtime:
/********** converting unix/posix time timestamp unix time: number of seconds since 1970-01-01 00:00:00 utc not counting leap seconds (currently 24 in 2011) working negative numbers. maximum range of timestamps based on range of integers: 1901-12-13 20:45:52 (-2147483648) 2038-01-19 03:14:07 (2147483647) can changed use bigint instead of integer 20101211 initial version - dieter noeth **********/ replace function epoch2timestamp (unixtime int) returns timestamp(0) language sql contains sql deterministic returns null on null input sql security definer collation invoker inline type 1 return cast(date '1970-01-01' + (unixtime / 86400) timestamp(0)) + ((unixtime mod 86400) * interval '00:00:01' hour second) ; select epoch2timestamp(-2147483648) ,epoch2timestamp(0) ,epoch2timestamp(2147483647) ;
but values seem include milliseconds, needs modified calculation:
cast(date '1970-01-01' + (unixtime / 86400000) timestamp(3)) + ((unixtime / 1000.000 mod 86400) * interval '00:00:01' hour second)
edit 2016-07-01:
there issue dayight saving time (see this thread on teradata's on devex), should fix it:
-- unix time timestamp time zone (+00:00) replace function unixtime_to_timestamp_tz (unixtime int) returns timestamp(0) time zone language sql contains sql deterministic sql security definer collation invoker inline type 1 return ((cast(date '1970-01-01' + (unixtime / 86400) timestamp(0) @ 0)) @ 0) + ((unixtime mod 86400) * interval '00:00:01' hour second); -- unixtime timestamp, implicit time zone of local session replace function unixtime_to_timestamp (unixtime int) returns timestamp(0) language sql contains sql deterministic sql security definer collation invoker inline type 1 return cast(((cast(date '1970-01-01' + (unixtime / 86400) timestamp(0) @ 0)) @ 0) + ((unixtime mod 86400) * interval '00:00:01' hour second) timestamp(0));
Comments
Post a Comment