I'm trying to change the datatype in a column in a table from Float
(null) to Varchar(25)
(null). The largest float in the current data is 12 digits, but more digits may have to be added in the future, hence varchar(25)
.
The current data in the column is phone numbers. The change must be made to allow for preceding zeros.
However, I'm having some difficulties doing this.
I've tried the following:
ALTER TABLE Customer
ALTER COLUMN Phonenumber varchar(25)
This does not give me the desired result.
For instance 1549779498
becomes 1.54978e+009
Then I've tried something in the lines of this:
- Creating a new (temporary) column
PhonenumberVarchar
- Converting and copying the data from one column to another
- Deleting the old column
- Renaming the new column to the old name
Code:
ALTER TABLE Customer
ADD PhonenumberVarchar varchar(25)
UPDATE Customer
SET PhonenumberVarchar = STR(Phonenumber, 12, 0)
ALTER TABLE Customer
DROP COLUMN Phonenumber
EXEC sp_rename 'Customer.PhonenumberVarchar', 'Phonenumber', 'COLUMN'
This doesn't work either:
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
And now it's late and my head hurts...
Can anyone help?
NOTE:
The table is rather large, some 1.5 million rows, so performance can be an issue.
Using SQL Server.