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

Sql ServerSql Server-2008Alter

Sql Server Problem Overview


I have SQL Server database and I just realized that I can change the type of one of the columns from int to bool.

How can I do that without losing the data that is already entered into that table?

Sql Server Solutions


Solution 1 - Sql Server

You can easily do this using the following command. Any value of 0 will be turned into a 0 (BIT = false), anything else will be turned into 1 (BIT = true).

ALTER TABLE dbo.YourTable
   ALTER COLUMN YourColumnName BIT

The other option would be to create a new column of type BIT, fill it from the old column, and once you're done, drop the old column and rename the new one to the old name. That way, if something during the conversion goes wrong, you can always go back since you still have all the data..

Solution 2 - Sql Server

ALTER TABLE tablename
ALTER COLUMN columnname columndatatype(size)

Note: if there is a size of columns, just write the size also.

Solution 3 - Sql Server

If it is a valid change.

you can change the property.

> Tools --> Options --> Designers --> Table and Database designers --> Uncheck --> Prevent saving changes that required table re-creation.

Now you can easily change the column name without recreating the table or losing u r records.

Solution 4 - Sql Server

if you use T-SQL(MSSQL); you should try this script:

ALTER TABLE [Employee] ALTER COLUMN [Salary] NUMERIC(22,5)

if you use MySQL; you should try this script:

ALTER TABLE [Employee] MODIFY COLUMN [Salary] NUMERIC(22,5)

if you use Oracle; you should try this script:

ALTER TABLE [Employee] MODIFY [Salary] NUMERIC(22,5)

Solution 5 - Sql Server

Why do you think you will lose data? Simply go into Management Studio and change the data type. If the existing value can be converted to bool (bit), it will do that. In other words, if "1" maps to true and "0" maps to false in your original field, you'll be fine.

Solution 6 - Sql Server

Go to Tool-Option-designers-Table and Database designers and Uncheck Prevent saving optionenter image description here

Solution 7 - Sql Server

Alter column data type with check type of column :

IF EXISTS(
       SELECT 1
       FROM   sys.columns
       WHERE  NAME = 'YourColumnName'
              AND [object_id] = OBJECT_ID('dbo.YourTable')
              AND TYPE_NAME(system_type_id) = 'int'
   )
    ALTER TABLE dbo.YourTable ALTER COLUMN YourColumnName BIT

Solution 8 - Sql Server

for me , in sql server 2016, I do it like this

*To rename column Column1 to column2

EXEC sp_rename 'dbo.T_Table1.Column1', 'Column2', 'COLUMN'

*To modify column Type from string to int:( Please be sure that data are in the correct format)

ALTER TABLE dbo.T_Table1 ALTER COLUMN Column2  int; 

Solution 9 - Sql Server

In compact edition will take size automatically for datetime data type i.e. (8) so no need to set size of field and generate error for this operation...

Solution 10 - Sql Server

I can modify the table field's datatype, with these following query: and also in the Oracle DB,

ALTER TABLE table_name
MODIFY column_name datatype;

Solution 11 - Sql Server

Replace datatype without losing data

alter table tablename modify columnn  newdatatype(size);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionIvan StoyanovView Question on Stackoverflow
Solution 1 - Sql Servermarc_sView Answer on Stackoverflow
Solution 2 - Sql ServerXyed Xain HaiderView Answer on Stackoverflow
Solution 3 - Sql ServerSathishView Answer on Stackoverflow
Solution 4 - Sql ServerNullExceptionView Answer on Stackoverflow
Solution 5 - Sql ServerPhilippe LeybaertView Answer on Stackoverflow
Solution 6 - Sql Serversaktiprasad swainView Answer on Stackoverflow
Solution 7 - Sql ServerEbrahim SabetiView Answer on Stackoverflow
Solution 8 - Sql ServerMNFView Answer on Stackoverflow
Solution 9 - Sql ServerHardikView Answer on Stackoverflow
Solution 10 - Sql ServerIsmayil SView Answer on Stackoverflow
Solution 11 - Sql Serveruser7743491View Answer on Stackoverflow