Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Permanently convert datetime to date

Tags:

postgresql

I have a postgres database with a datetime field. I'd like to permanently convert it to a date field (i.e. remove the time element). What is the best way to go about that? Is there a way to do it in place without having to dump the table into a new table?

like image 509
Jude Osborn Avatar asked Oct 15 '25 23:10

Jude Osborn


2 Answers

Assuming you mean a timestamp with "datetime".

ALTER TABLE foo ALTER COLUMN bar TYPE date;
like image 57
plundra Avatar answered Oct 18 '25 18:10

plundra


Create new temporary field and UPDATE yourtable SET newfield = oldfield::date with proper conversion function and then just remove old field and rename the new field.

like image 26
raspi Avatar answered Oct 18 '25 18:10

raspi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!