SQL alter column datatype from nvarchar to int

Mohit Jain picture Mohit Jain · Aug 28, 2013 · Viewed 91.1k times · Source

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

Answer

dcaswell picture dcaswell · Aug 28, 2013

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