首页 > 解决方案 > 将 Postgres 文本字段转换为时间戳字段时出现问题忽略格式

问题描述

Postgres 90611
尝试使用以下 SQL 将字符串字段转换(file_original_create_datetime)为新的时间戳字段(new_original_create_datetime)时,结果仅包括时间,而不包括日期。

update files  
set new_original_create_datetime = to_timestamp(file_original_create_datetime, 'YYYY-MM-DD hh24:mi:ss') ::timestamp without time zone at time zone 'Etc/UTC'

输入数据

标签: postgresql

解决方案


在这里工作:

select to_timestamp('2019-02-28 21:15:01', 'YYYY-MM-DD hh24:mi:ss')::timestamp without time zone at time zone 'Etc/UTC';                                                                
+------------------------+
| timezone               |
|------------------------|
| 2019-02-28 22:15:01+01 |
+------------------------+

如果列的类型new_original_create_datetimetimestamp,则 UPDATE应该可以。


推荐阅读