This is similar to a previous question MSSQL: Change type of a column with numbers from varchar to int, except in this case not all of the original data can be successfully converted from varchar to int. In these cases, I want the output to just be NULL
.
For example, the incoming set of values in a varchar column will look like: {'123', '234', '345', 'A', '456'}
. After conversion from varchar to int, I want the column to contain the values {123, 234, 345, NULL, 456}
. In practice, I'll have about 99% success rate.
While a general solution would be ideal, I am using SQL Server 2005 and need to perform the change in Transact SQL.
EDIT: The IsNumeric
suggestions do bridge a portion of the gap, but are not complete. For example, if the data set includes numeric but not integer data, the IsNumeric
fix doesn't work (and there doesn't appear to be a IsInteger
). In particular, the following set will not convert {'123.456', '$12'} where they are both numeric according to SQL Server IsNumeric
but will not convert to Int
.