SQL 如何将列类型从 varchar 转换为 PostgreSQL 中的日期?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2592412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert column type from varchar to date in PostgreSQL?
提问by chandrasekhar
I have varchar
data type column and date
data type column.
我有varchar
数据类型列和date
数据类型列。
I have to update varchar
column data into date
column in PostgreSQL.
我必须将varchar
列数据更新为date
PostgreSQL 中的列。
Is it possible?
是否可以?
Thanks.
谢谢。
回答by Alexander Monteiro
ALTER TABLE <tablename> ALTER COLUMN <columnname> TYPE DATE
using to_date(<columnname>, 'YYYY-MM-DD');
回答by Salil
UPDATE tableName SET dateColumn=to_date(varcharColumn, 'DD MM YYYY')
Assuming you are saving "07 04 2010"
假设您正在保存“07 04 2010”
You can find further examples and explanation in the documentation:
您可以在文档中找到更多示例和说明:
http://www.postgresql.org/docs/current/interactive/functions-formatting.html
http://www.postgresql.org/docs/current/interactive/functions-formatting.html
回答by MJB
to_date('05 Dec 2000', 'DD Mon YYYY')
回答by Satish
syntax for typecasting:
类型转换的语法:
alter table table_name alter column_name
type converting_data_type using(column_name::converting_data_type)
converting from varchar to date
从 varchar 转换为日期
alter table table_name
alter column_name type date using(column_name::date)