有没有办法在 Postgres 中将 BIGINT
转换为 TIMESTAMP
或 TIMESTAMP WITH TIME ZONE
?我需要将数据从 BIGINT
列复制到 TIMESTAMP
列。
这是我尝试过的:
update table
set date__timestamp = date__bigint::timestamp
where foo = 1;
ERROR: cannot cast type bigint to timestamp without time zone
我将时间戳列更改为带有时区的列并尝试了这个:
update table
set date__timestamp = date__bigint::timestamp with time zone at time zone 'PST'
where foo = 1;
ERROR: cannot cast type bigint to timestamp with time zone
update table
set date__timstamp = TO_CHAR(TO_TIMESTAMP(date__bi / 1000), 'DD/MM/YYYY HH24:MI:SS')
where foo = 1;
ERROR: column "date__timestamp" is of type timestamp without time zone but expression is of type text Hint: You will need to rewrite or cast the expression.
数据看起来像这样。
date_bigint: 20181102
date_timestamp: 2018-11-02 17:00:00.000000
我需要将默认值传递给转换吗?
最佳答案
您可以将其转换为文本并使用 TO_TIMESTAMP
并转换为 timestamp at time zone
SELECT
to_timestamp ( '20181102'::bigint::text,'YYYYMMDD')::timestamp at time zone 'UTC'
at time zone 'PST' ;
update t
set date__timestamp = TO_TIMESTAMP(date_bigint::text,'YYYYMMDD')::timestamp
at time zone 'UTC' at time zone 'PST'
where foo = 1;
关于sql - 如何在更新中将 bigint 转换为带时区的时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54193335/