Hey I have an SQL Table which has a column for storing date but the date column has a type varchar. I want to change the type to date but I don’t want the actual data to be lost in that column. How can I achieve that.
Manually taking a backup of the table and then entering each entry? or there is some other cool way to do it ? Actually the data is huge
Thanks
My way of doing this:
(1) Add a new column:
(2) Update the new column
Take care of the datas formatting in
old_date. If it isn’t formattedyyyy-mm-dd, you might have toSTR_TO_DATEor some string-replacements in thisUPDATE-statement here to fit your purposes.Example:
If your data looks like this:
mmmm dd, yyyy, hh:mm(p.e.May 17, 2012, 8:36 pm) , you can update like this:STR_TO_DATEbasically reverse engineers string data to a date value.(3) Delete the old column
(4) Rename the new column
Done!