sql - 如何在更新中将 bigint 转换为带时区的时间戳

标签 sql postgresql datetime

有没有办法在 Postgres 中将 BIGINT 转换为 TIMESTAMPTIMESTAMP 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;

Demo

关于sql - 如何在更新中将 bigint 转换为带时区的时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54193335/

相关文章:

mysql - 在 MySQL 中分组时如何获取具有最大日期的行?

mysql - 如何使用 select where in 作为所有必需的输入

r - 多个 selectInput 值会产生意外的 dplyr (postgres) 行为

postgresql - 在 SQL 控制台中运行 PostgreSQL 存储过程

mysql - 将日期时间列转换为 24 小时格式

mysql 加载数据内文件,不带字段终止

sql - 如何在SQL Server 2005/8上获取类表查询结果?

postgresql - 如何使用ansible设置postgres密码

javascript - 在 jQuery 中将年、月和日转换为单个日期

python - 类型错误 : '>=' not supported between instances of 'datetime.date' and 'str'