223

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?

3
  • 3
    Have you tried creating a new bit column, copying the values from the old column into the new one, deleting the old one and renaming the new one? All this in a transaction, of course, to rollback on problems. Commented Feb 27, 2011 at 21:19
  • 2
    You say "I just realized that I can change the type from one of the columns from int to bool" There is no boolean datatype. There is bit though. Are you asking how you can do this (as the 2 answers so far have covered). Or is your question "I just realised this is possible - How does SQL Server do this?" Commented Feb 27, 2011 at 21:28
  • 1
    possible duplicate of How do you change the datatype of a column in MS SQL? Commented Dec 20, 2012 at 13:21

11 Answers 11

363

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..

Sign up to request clarification or add additional context in comments.

3 Comments

In other words: NULL remains NULL, 0 becomes False, non-zero values (1, -1, 1999, -987...) become True.
And never make a change like this from the GUI. Always make it through a script like this. The GUI will drop and recreate the table and that is much more time consuming. If the table is large and on production, this can be disastrous. Plus all table changes should have a script that is in source control like all other code.
I would add, is make sure you have a current backup of the database before making any structural change to a table with data. And do not run a change like this on production during peak usage hours if the table is frequently used or large.
28
ALTER TABLE tablename
ALTER COLUMN columnname columndatatype(size)

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

Comments

24

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.

3 Comments

Under no circumstances should you be making table changes using the GUI. It will completely recreate the table rather than use Alter table and this will cause an issue if you you uncheck this and the table is large. Further you should have all changes to tables in a script in source control.
Look more closely at that option -- it's turning off a safety that will prevent GUI from dropping the table. You won't appear to lose data, because the GUI will recreate the table, but on the server it will be copy/drop. So, if there is a lot of data in the table, it will cause a very large operation. Also, I think (not positive) that it occurs in a single transaction, therefore you could fill your transaction log.
I guess I'd like to add a little clarification to my previous comment. If you are just doing this on your development machine, you are probably fine. But I would not advise using this method on a production database -- especially if it is a mission critical one.
11

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)

Comments

8

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.

6 Comments

If you have data in the table this won't work. When you attempt to change a column type, SMS claims that it needs to drop the table first ... which of course is incorrect since the ALTER TABLE ... ALTER COLUMN command works just fine even for non NULL fields. This is why they thought they could loose data.
@TonyO'Hagan That's not true. You can turn off the warning and it will work just fine with existing data. See also stackoverflow.com/questions/2947865/…
Ok cool! Didn't know that. Just tried to up vote you (back) but SO is preventing me unless you edit your answer. Perhaps a minor change and I can do so ;) .
Under no circumstances should you be making table changes using the GUI. It will completely recreate the table rather than use Alter table and this will cause an issue if you you uncheck this and the table is large. Further you should have all changes to tables in a script in source control.
@HLGEM That's what database projects are for.
|
4

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

1 Comment

Under no circumstances should you be making table changes using the GUI. It will completely recreate the table rather than use Alter table and this will cause an issue if you you uncheck this and the table is large. Further you should have all changes to tables in a script in source control.
4

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; 

Comments

4

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

Comments

0

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...

Comments

-3

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;

2 Comments

Not in SQl Server
This should be ALTER COLUMN instead of MODIFY
-5

Replace datatype without losing data

alter table tablename modify columnn  newdatatype(size);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.