在我的Teradata表格中,我在dhTimestamp
列
dhTimestamp
1435308067705
1434965874565
1434763800794
1434775876034
1434765207057
如何在Teradata上将纪元时间戳转换为人类日期/时间格式?
答案 0 :(得分:4)
这是标准unixtime的SQL UDF:
/**********
Converting Unix/POSIX time to a Timestamp
Unix time: Number of seconds since 1970-01-01 00:00:00 UTC not counting leap seconds (currently 24 in 2011)
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)
Can be changed to 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) AS TIMESTAMP(0))
+ ((UnixTime MOD 86400) * INTERVAL '00:00:01' HOUR TO SECOND)
;
SELECT
Epoch2Timestamp(-2147483648)
,Epoch2Timestamp(0)
,Epoch2Timestamp(2147483647)
;
但是您的值似乎包含毫秒,这需要修改计算:
CAST(DATE '1970-01-01' + (UnixTime / 86400000) AS TIMESTAMP(3))
+ ((UnixTime / 1000.000 MOD 86400) * INTERVAL '00:00:01' HOUR TO SECOND)
日常节省时间存在问题(请参阅有关DevEx的Teradata上的this thread),这应该解决它:
-- Unix time to Timestamp WITH TIME ZONE (+00:00)
REPLACE FUNCTION UnixTime_to_TimeStamp_TZ (UnixTime INT)
RETURNS TIMESTAMP(0) WITH TIME ZONE
LANGUAGE SQL
CONTAINS SQL
DETERMINISTIC
SQL SECURITY DEFINER
COLLATION INVOKER
INLINE TYPE 1
RETURN
((CAST(DATE '1970-01-01' + (UnixTime / 86400) AS TIMESTAMP(0) AT 0)) AT 0)
+ ((UnixTime MOD 86400) * INTERVAL '00:00:01' HOUR TO SECOND);
-- Unixtime to Timestamp, implicit TIME ZONE of the 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) AS TIMESTAMP(0) AT 0)) AT 0)
+ ((UnixTime MOD 86400) * INTERVAL '00:00:01' HOUR TO SECOND) AS TIMESTAMP(0));
答案 1 :(得分:0)
我可以使用以下查询将纪元列转换为时间戳。
if (Friends != null) {
Friends.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
friend_source = "Friend";
Newspaper.setChecked(false);
Facebook.setChecked(false);
Zomato.setChecked(false);
RadioSource.setChecked(false);
}
}
});
}
只需将 epochtimecolumn 替换为上述查询中的列即可在teradata中运行它。
希望有帮助!
答案 2 :(得分:0)
这是将EPOCH转换为TERADATA LOCAL的最简单方法。
SELECT
dhTimestamp as unix_epoc_time ,
to_timestamp(unix_epoc_time) utc,
cast(cast(utc as char(19))||'+00:00' as timestamp(0) with time zone) AT LOCAL
utc
视为本地。因此,让我们通过添加'+00:00'
来理解它是UTC,然后使用AT LOCAL
或使用以下两个“ America Central”,“ America Eastern”,“ America Mountain”将其转换为本地等。