我想将一列数据从文本更改为类型时间戳。我的数据中没有时区。我的数据格式如28-03-17 17:22,包括时间和日期,但没有时区。换句话说,我的所有数据都在同一时区。我该怎么做?
我在下面尝试了多种方法,但我仍然找不到合适的方法。希望你能帮帮我。
当然,如果我的麻烦得以解决,我可以建立一个新的桌子。
alter table AB
alter create_time type TIMESTAMP;
ERROR: column "create_time" cannot be cast automatically to type timestamp without time zone
HINT: 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 zone
SQL state: 42804
Hint: You might need to specify "USING create_time::timestamp without time zone".
alter table AB
alter create_time type TIMESTAMP without time zone;
ERROR: column "create_time" cannot be cast automatically to type timestamp without time zone
HINT: 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 zone
SQL state: 42804
Hint: You might need to specify "USING create_time::timestamp without time zone".
alter table AB
alter 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: 42601
Character: 50
alter table AB
alter 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: 42601
Character: 50
答案 0 :(得分:3)
USING...
出现在类型:
... alter create_time type TIMESTAMP USING create_time::TIMESTAMP;
答案 1 :(得分:3)
如果create_time
的TEXT类型具有有效的日期值,则更容易按如下方式继续进行更改(建议首先将表转储作为备份):
-- 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 create_time_holder;
答案 2 :(得分:1)
您没有指定create_time的原始类型,因此我假设它是带时区的TIME(因为DATE类型或带有时区的TIMESTAMP在尝试更改为没有时区的TIMESTAMP时不应该给出所述错误)。由于TIMESTAMP除了TIME之外还有日期信息,因此您需要在ALTER语句中补充日期信息,例如:
ALTER TABLE AB ALTER COLUMN create_time TYPE TIMESTAMP without time zone USING date('20170327') + create_time;
如果你有一个相应的DATE列(比如create_date),你可以将它传递给date()函数,如:
ALTER TABLE AB ALTER COLUMN create_time TYPE TIMESTAMP without time zone USING date(create_date) + create_time;
答案 3 :(得分:-1)
用于将列的数据类型从bigint更改为时间戳(对于纪元到时间戳)
alter table <tablename> alter column <columnname> type timestamp without time zone using to_timestamp(<columnname>) AT TIME ZONE 'UTC';
例如;
alter table AB alter column col type timestamp without time zone using to_timestamp(col) AT TIME ZONE 'UTC';
用于将列的数据类型从timestamp更改为bigint(用于时间戳到纪元)
alter table <tablename> alter column <columnname> type bigint using extract(EPOCH from <columnname>);
例如;
alter table AB alter column col type bigint using extract(EPOCH from col);