Teradata中的时间戳到时代的转换

我在teradata表中有一个TIMESTAMP列.我想将时间戳转换为纪元值.有人可以阐明如何做到这一点.
最佳答案
这是我几年前写的SQL UDF.

如果您没有创建功能的访问权限,则可以要求您轻按或简单地剪切&粘贴计算.

/**********
Converting a Timestamp to Unix/POSIX/epoch time

Unix time: Number of seconds since 1970-01-01 00:00:00 UTC not counting leap seconds (currently 34 in 2010)

The maximum range of Timestamps is based on the range of INTEGERs:
1901-12-13 20:45:52 (-2147483648) to 2038-01-19 03:14:07 (2147483647)

Simply change to BIGINT to cover the full Teradata date range

20101211 initial version - Dieter Noeth
**********/
REPLACE FUNCTION TimeStamp_to_UnixTime (ts TIMESTAMP(6))
RETURNS INT
LANGUAGE SQL
CONTAINS SQL
DETERMINISTIC
SQL SECURITY DEFINER
COLLATION INVOKER
INLINE TYPE 1
RETURN 
(CAST(ts AS DATE) - DATE '1970-01-01') * 86400
+ (EXTRACT(HOUR FROM ts) * 3600)
+ (EXTRACT(MINUTE FROM ts) * 60)
+ (EXTRACT(SECOND FROM ts))
;

反转计算:

/**********
Converting Unix/POSIX/epoch time to a Timestamp 

Unix time: Number of seconds since 1970-01-01 00:00:00 UTC not counting leap seconds (currently 34 in 2010)

Also working for negative numbers.
The maximum range of Timestamps is based on the range of INTEGERs:
1901-12-13 20:45:52 (-2147483648) to 2038-01-19 03:14:07 (2147483647)

Simply change to BIGINT to cover the full Teradata date range

20101211 initial version - Dieter Noeth
**********/

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(DATE '1970-01-01' + (UnixTime / 86400) AS TIMESTAMP(0))
+ ((UnixTime MOD 86400) * INTERVAL '00:00:01' HOUR TO SECOND)
;

在TD14中使用TO_TIMESTAMP(UnixTime)更容易,但这仅限于INTEGER范围.

转载注明原文:Teradata中的时间戳到时代的转换 - 代码日志