postgreSQL将列数据类型改为没有时区的时间戳
技术问答
481 人阅读
|
0 人回复
|
2023-09-14
|
我想把一列数据从文本改为时间戳类型。我的数据中没有时区。我的数据格式类似于28-03-17
& q/ ]* a: u4 c b0 O, [17:22,包括时间和日期,但没有时区。换句话说,我所有的数据都在同一时区。我该怎么办?2 h( e( {# ]6 T% m: J
我在下面尝试了很多方法,但我还是找不到正确的方法。我希望你能帮助我。
1 a: c; o- o& L当然,如果我能解决我的问题,我可以建立一个新的手表。
# }, N; F6 r/ V5 B2 M5 d8 xalter table ABalter create_time type TIMESTAMP;ERROR: column "create_time" cannot be cast automatically to type timestamp without time zoneHINT: You might need to specify "USING create_time::timestamp without time zone".********** Error **********ERROR: column "create_time" cannot be cast automatically to type timestamp without time zoneSQL state: 42804Hint: You might need to specify "USING create_time::timestamp without time zone".alter table ABalter create_time type TIMESTAMP without time zone;ERROR: column "create_time" cannot be cast automatically to type timestamp without time zoneHINT: You might need to specify "USING create_time::timestamp without time zone".********** Error **********ERROR: column "create_time" cannot be cast automatically to type timestamp without time zoneSQL state: 42804Hint: You might need to specify "USING create_time::timestamp without time zone".alter table ABalter create_time::without time zone type TIMESTAMP;ERROR: syntax error at or near "::"LINE 2: alter create_time::without time zone type TIMESTAM ^********** Error **********ERROR: syntax error at or near "::"SQL state: 42601Character: 50alter table ABalter create_time UTC type TIMESTAMP;ERROR: syntax error at or near "UTC"LINE 2: alter create_time UTC type TIMESTAMP; ^********** Error **********ERROR: syntax error at or near "UTC"SQL state: 42601Character: 5050
; ^' p' q, [ c. A8 H* ?! A 解决方案: 5 D9 s6 |- p8 P# s! V. v
如果create_timeTEXT如果类型文本具有有效的日期值,则更容易按以下步骤进行更改(建议先将表转储作为备份):
: }2 |9 B2 \5 G E-- Create a temporary TIMESTAMP columnALTER TABLE AB ADD COLUMN create_time_holder TIMESTAMP without time zone NULL;-- Copy casted value over to the temporary columnUPDATE AB SET create_time_holder = create_time::TIMESTAMP;-- Modify original column using the temporary columnALTER TABLE AB ALTER COLUMN create_time TYPE TIMESTAMP without time zone USING create_time_holder;-- Drop the temporary column (after examining altered column values)ALTER TABLE AB DROP COLUMN create_time_holder; |
|
|
|
|
|