将Unix时间戳转换为时间码?

时间:2018-04-05 18:00:28

标签: sql amazon-redshift amazon-athena

我正在使用Athena Redshift SQL编写查询

我有两个不同的表格,我需要加入两列:unix_timestamptimestamp_code

unix_timestamp存储为string,如下所示:2018-04-01T10:05:52.047Z

timestamp_code string看起来像1514564885

我想将unix_timestamp转换为`timestamp_code当前所在的格式。

我将如何做到这一点?

1 个答案:

答案 0 :(得分:0)

转换为纪元:

SELECT EXTRACT(EPOCH FROM '2018-04-01T10:05:52.047Z'::TIMESTAMP);
-- date_part
--------------
-- 1522577152 

从纪元转换为时间戳:

SELECT '1970-01-01'::TIMESTAMP + 1522577152 * INTERVAL '1 sec';
--      ?column?
-----------------------
-- 2018-04-01 10:05:52

https://docs.aws.amazon.com/redshift/latest/dg/r_EXTRACT_function.html https://docs.aws.amazon.com/redshift/latest/dg/r_Dateparts_for_datetime_functions.html