Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to alter a SQL Server table column datatype from bigint to varchar after it has been populated?

I have a SQL Server 2008 table which contains an external user reference currently stored as a bigint - the userid from the external table. I want to extend this to allow email address, open ID etc to be used as the external identifier. Is it possible to alter the column datatype from bigint to varchar without affecting any of the existing data?

like image 320
Caroline Orr Avatar asked Dec 28 '22 01:12

Caroline Orr


1 Answers

Yes, that should be possible, no problem - as long as you make your VARCHAR field big enough to hold you BIGINT values :-)

You'd have to use something like this T-SQL:

ALTER TABLE dbo.YourTable
  ALTER COLUMN YourColumnName VARCHAR(50)   -- or whatever you want

and that should be it! Since all BIGINT values can be converted into a string, that command should work just fine and without any danger of losing data.

like image 184
marc_s Avatar answered May 13 '23 22:05

marc_s