Skip to content
Advertisement

How to change datatype of column with data

Let’s say I have a table call_log that has a column named duration. Let’s further pretend that when I put the table together, I made the mistake of making duration a varchar column instead of a number. Now I can’t properly sort on that column. I want to rename the column so I issue…

But I get…

My table has been in use and has data! And I don’t want to lose the data. How do I modify the column’s datatype without losing data?

Advertisement

Answer

Create a temporary column with the correct data type, copy the data to the new column, drop the old column, rename the new column to match what the old column was named.

The idea for this answer came from Oracle’s forums.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement