Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert column type from varchar to date in PostgreSQL?

Tags:

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

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

Is it possible?

Thanks.

like image 661
chandrasekhar Avatar asked Apr 07 '10 12:04

chandrasekhar


People also ask

How do I convert varchar to date in PostgreSQL?

The TO_DATE function in PostgreSQL is used to converting strings into dates. Its syntax is TO_DATE(text, text) and the return type is date. The TO_TIMESTAMP function converts string data into timestamps with timezone. Its syntax is to_timestamp(text, text) .

How do I change the datatype of a column in PostgreSQL?

First, specify the name of the table to which the column you want to change belongs in the ALTER TABLE clause. Second, give the name of column whose data type will be changed in the ALTER COLUMN clause. Third, provide the new data type for the column after the TYPE keyword.

What is the datatype of date in PostgreSQL?

The date format for the date data type in PostgreSQL is yyyy-mm-dd . This is the format used for both storing data and for inserting data.


2 Answers

ALTER TABLE <tablename> ALTER COLUMN <columnname> TYPE DATE 
using to_date(<columnname>, 'YYYY-MM-DD');
like image 62
Alexander Monteiro Avatar answered Oct 12 '22 11:10

Alexander Monteiro


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

Assuming you are saving "07 04 2010"

You can find further examples and explanation in the documentation:

http://www.postgresql.org/docs/current/interactive/functions-formatting.html

like image 21
Salil Avatar answered Oct 12 '22 11:10

Salil