Saturday, May 18, 2013

How to change column datatype in SQL Server database without losing data

 Suppose in SQL table "Users", DateOfBith is a field of datatype datetime. If you want to change the datatype to varchar or any other. Then you have to use the below query

Alter table Users alter column DateOfBith varchar(20)

Note:- In some case this will not work if you want to change the datatype from varchar to Int and existing column contains the alphanumeric values. It should be blank or int type values only then you can change the column datatype.