首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >postgreSQL将列数据类型更改为没有时区的时间戳

postgreSQL将列数据类型更改为没有时区的时间戳
EN

Stack Overflow用户
提问于 2017-03-28 01:26:44
回答 4查看 80.7K关注 0票数 27

我希望将一列数据从文本更改为时间戳类型。我的数据中没有时区。我的数据格式是28-03- 17 :22,包括时间和日期,但没有时区。换句话说,我所有的数据都在同一个时区。我该怎么做呢?

我在下面尝试了多种方法,但我仍然找不到正确的方法。希望你能帮我。

当然,如果我的麻烦能解决的话,我可以建一张新桌子。

代码语言:javascript
运行
复制
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".
代码语言:javascript
运行
复制
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".
代码语言:javascript
运行
复制
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
代码语言:javascript
运行
复制
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
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2017-03-29 02:25:17

如果create_time的文本类型为有效日期值,将更容易按以下方式进行更改(建议首先将表转储作为备份):

代码语言:javascript
运行
复制
-- 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;
票数 37
EN

Stack Overflow用户

发布于 2017-03-28 06:22:01

USING...在以下类型之后出现:

代码语言:javascript
运行
复制
... alter create_time type TIMESTAMP USING create_time::TIMESTAMP;
票数 13
EN

Stack Overflow用户

发布于 2017-03-28 06:22:52

您没有指定原始类型的create_time,所以我假设是时区时间(因为类型日期或时区时间戳在试图更改为没有时区的时间戳时不应该给出上述错误)。由于时间戳除了时间之外还有日期信息,所以您需要在ALTER语句中补充您的日期信息,例如:

代码语言:javascript
运行
复制
ALTER TABLE AB ALTER COLUMN create_time TYPE TIMESTAMP without time zone USING date('20170327') + create_time;

如果有相应的DATE列(例如,create_date),可以将其传递给date()函数,如下所示:

代码语言:javascript
运行
复制
ALTER TABLE AB ALTER COLUMN create_time TYPE TIMESTAMP without time zone USING date(create_date) + create_time;
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43059108

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档