如何在PostgreSQL中将列类型从varchar转换为date ?

时间:2022-06-18 16:33:13

I have varchar data type column and date data type column.

我有varchar数据类型列和日期数据类型列。

I have to update varchar column data into date column in PostgreSQL.

我必须在PostgreSQL中将varchar列数据更新为日期列。

Is it possible?

是可能的吗?

Thanks.

谢谢。

4 个解决方案

#1


39  

ALTER TABLE <tablename> ALTER COLUMN <columnname> TYPE DATE 
using to_date(<columnname>, 'YYYY-MM-DD');

#2


5  

UPDATE tableName SET dateColumn=to_date(varcharColumn, 'DD MM YYYY')

Assuming you are saving "07 04 2010"

假设你正在保存“0704 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

#3


3  

to_date('05 Dec 2000', 'DD Mon YYYY')

#4


1  

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)

#1


39  

ALTER TABLE <tablename> ALTER COLUMN <columnname> TYPE DATE 
using to_date(<columnname>, 'YYYY-MM-DD');

#2


5  

UPDATE tableName SET dateColumn=to_date(varcharColumn, 'DD MM YYYY')

Assuming you are saving "07 04 2010"

假设你正在保存“0704 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

#3


3  

to_date('05 Dec 2000', 'DD Mon YYYY')

#4


1  

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)