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

268 Views Asked by At

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?

1

There are 1 best solutions below

0
On BEST ANSWER

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;