无法从毫秒时代 postgresql

问题描述

我正在查询数据库 (RedShift) 并且我有一条以纪元 MS 格式存储的信息。设想一个表格:

Purchase,date
1,1620140227019
2,1620140227045
3,1620140226573

我需要将时间戳转换为可读日期,但我无法使用 to_timestamp() 或 extract() 使其工作。问题首先在于值的大小(不支持 13 位数字)。 我最接近的解决方案是

select  to_timestamp(1620140226573/1000,'SS')

但结果是0051-05-04 14:57:06。换句话说,月份、日期和秒是正确的,但年份是错误的。

解决方法

您可以运行此查询

select to_timestamp(round(1620140227254/1000))
,

解决方案在文档中:https://docs.aws.amazon.com/redshift/latest/dg/r_Dateparts_for_datetime_functions.html

SELECT timestamp with time zone 'epoch' + 1620140227019/1000 * interval '1 second' AS converted_timestamp

select '1970-01-01'::date + 1620140227019/1000 * interval '1 second'