Search code examples
postgresqlsqldatetime

Convert a column of type varchar(255) to type date in PostgreSQL


I'm a new in SQL and I'm trying to convert a column of type varchar(255) to datetime (yyyy-mm-dd). I found some advice to convert one element but how can I convert entire column to a new datatype?

My table is mydemo and the column to convert is ordertime and it contains only valid datetime strings lie this:

  • 2021-05-13
  • 2021-05-12
  • 2021-05-10

Could someone please help me?


Solution

  • if all the rows have valid datetime values you can do it using command below:

    alter table mydemo
    alter column ordertime type timestamp USING ordertime::timestamp without time zone;