Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change column type bit to char 2

Status
Not open for further replies.

micang

Technical User
Aug 9, 2006
626
US
SQL 2005

Hi All,

I have inherited a table i SQL 2005. One of the columns is a "bit" type and has 1 or 0 as the data. I would like to change this to a char (1) as I need to add 2 more variants (I need to show,0 or 1 or 2 or 3) in the column.

Is it possible to do this or would I have to create a new column? The table has 45 million rows.

Any info apprecited.

Michael
 
I would suggest you change the data type to tinyint or int.

Code:
[COLOR=blue]Create[/color] [COLOR=blue]Table[/color] #Temp (Data [COLOR=blue]Bit[/color])

[COLOR=blue]Insert[/color] [COLOR=blue]Into[/color] #Temp [COLOR=blue]Values[/color](1)
[COLOR=blue]Insert[/color] [COLOR=blue]Into[/color] #Temp [COLOR=blue]Values[/color](0)

[COLOR=blue]Select[/color] * [COLOR=blue]From[/color] #Temp

[COLOR=blue]Alter[/color] [COLOR=blue]Table[/color] #Temp [COLOR=blue]Alter[/color] [COLOR=blue]Column[/color] Data [COLOR=blue]TinyInt[/color]

[COLOR=blue]Select[/color] * [COLOR=blue]From[/color] #Temp

[COLOR=blue]Drop[/color] [COLOR=blue]Table[/color] #Temp

-George

"the screen with the little boxes in the window." - Moron
 
alter table TableName alter column ColumnName char(1)


example

Code:
create table testBla (id bit)
go
insert testBla values(1)

select * from testBla
go
alter table testBla alter column id char(1)
go
insert testBla values(3)


select * from  testBla 

drop table testBla

Denis The SQL Menace
--------------------
SQL Server Code,Tips and Tricks, Performance Tuning
SQLBlog.com, Google Interview Questions
 
Thanks! Double confirmation!

Michael
 
Apologies, one more thing:

I have a fear of starting this on a 45 million rows and am afraid it will "hang".

I have tested it on a 50,000 row table, and it took seconds, but will it be OK on 45 million?

Reason I have this fear is that I have had little success in updating large tables and would like to not encounter the same thing here!

PS - Is there an easy way of updating columns in batches?

Many thanks

Michael
 
Thank you, I ran the query took 24 minutes.

Appreciate your help.

Michael
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top