Skip to content
Advertisement

Oracle: How to change column data type from VARCHAR to NUMBER without losing data

I have the following columns in my table:

  • ID - NUMBER(10,0)
  • NUMBER - VARCHAR(255)

All data in the NUMBER column are numbers. I would like to change the VARCHAR type to Integer Number type. How to do it without data loss?

Advertisement

Answer

Oracle does not allow modification of data type of the column if it is not empty so as a workaround, You need to follow the following steps

  1. create another column with NUMBER data type let’s say "NUMBER1".
  2. add the data of "NUMBER" column into that newly created column("NUMBER1").
  3. Remove the original "NUMBER" column
  4. Rename the newly created column "NUMBER1" to "NUMBER"

as following:

Oracle setup:

Showing error if the column data type is changed directly:

Solution:

Cheers!!

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