How do I change column type from varchar to numeric which has data already in it, and the data is numbers.
SQL SERVER
How do I change column type from varchar to numeric which has data already in it, and the data is numbers.
SQL SERVER
Something like this,
But as mentioned in the comments you need to be aware of triggers.FKs etc Updated to use TRY_CONVERT
ALTER TABLE dbo.table ADD numeric_column(x,y); --x, y here is the precision, scale
GO
UPDATE dbo.table
SET numeric_column = TRY_CONVERT(NUMERIC(x,y),varchar_numbers_column)
GO
ALTER TABLE DROP COLUMN varchar_numbers_column
GO
Using Try_Convert, returns NULL when conversion fails
SELECT
Failed_Date = TRY_CONVERT(NUMERIC(12,2), '12/31/2010')
,Failed_# = TRY_CONVERT(NUMERIC(12,2), '#')
,WorkingWholeNumber = TRY_CONVERT(NUMERIC(12,2), '3')
,WorkingDecimal = TRY_CONVERT(NUMERIC(12,2), '5.67')