Search code examples
sql-servertype-conversionalter-table

SQL alter column datatype from nvarchar to int


Can the datatype of a field be changed to int from nvarchar??

alter table employee alter column designation int

is this valid?? If not can it be done in some other way??

P.S: I am using MS SQL Server


Solution

  • You can try doing an alter table. If it fails do this:

    1. Create a new column that's an integer:

    ALTER TABLE tableName ADD newCol int;

    1. Select the data from the old column into the new one:

    UPDATE tableName SET newCol = CAST(oldCol AS int);

    1. Drop the old column