cannot be cast automatically to type timestamp without time zon

一个字段是varchar类型,里面存的都是日期值,没有不符合日期的格式的数据,但是直接把字段varchar-->timestamp就会报错,以前mysql里面如果数据都是正常的,可以直接转,应该是mysql帮做了内部转换。

现在的步骤为:

(1)先加一个临时列,类型为需要的类型timestamp,然后把之前那个字段的值::timestamp以后更新到新家的字段。

(2)删掉原来的列,把新家的列名字改回去。

-- Create a temporary TIMESTAMP column
ALTER TABLE AB ADD COLUMN create_time_holder TIMESTAMP without time zone NULL;

-- Copy casted value over to the temporary column
UPDATE AB SET create_time_holder = create_time::TIMESTAMP;

-- Modify original column using the temporary column
ALTER 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 

  

posted @ 2020-11-14 11:56  5sdba  阅读(758)  评论(0编辑  收藏  举报