gpt4 book ai didi

ruby-on-rails-3 - 将列类型从字符变化更改为不带时区的时间戳

转载 作者:行者123 更新时间:2023-11-29 11:29:49 26 4
gpt4 key购买 nike

在 Rails 中,我创建了一个名为 open_time 的字符串列,但后来我意识到我应该使用 datetime 类型。我做了:

change_column :polls, :open_time, :datetime

但是它说:

PG::Error: ERROR: column "open_time" cannot be cast to type timestamp without time zone
: ALTER TABLE "polls" ALTER COLUMN "open_time" TYPE timestamp

如果我只是删除字符串列并添加新的日期时间列,我将丢失存储在字符串列中的数据。或者,在 PostgreSQL 中我可以添加一列:

ALTER TABLE polls ADD COLUMN published_time timestamp;

Ant 然后尝试从字符串列中获取数据,如:

UPDATE polls SET published_time = strToTimeStamp(open_time);

是否有任何函数可以作为 strToTimeStampcharacter varying 类型转换为 timestamp without time zone

最佳答案

.. are there any functions I can use as strToTimeStamp that can convertcharacter varying type to timestamp without time zone type?

使用to_timestamp()将字符串数据转换为 timestamp 类型并更改列的数据类型:

ALTER TABLE tbl ALTER COLUMN col TYPE timestamp
USING to_timestamp(col, '<your pattern here>');

参见:

关于ruby-on-rails-3 - 将列类型从字符变化更改为不带时区的时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13372825/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com