I have a column that contains characters and numbers
12
13
14
19K/YR
22
So the column type is varchar. But then I'm also doing some computations with this column, so I'm trying to convert the data to float if it is numeric.
This gives me an error though:
SELECT CASE ISNUMERIC(QTY)
WHEN 1 THEN CAST(QTY AS float)
ELSE QTY
END AS test
If you are planning to convert varchar to float you should know that these two data types are not compatible with each other.
As the name suggests, varchar means character data that is varying. Also known as Variable Character, it is an indeterminate length string data type. It can hold numbers, letters and special characters.
You can't cast to float and keep the string in the same column. You can do like this to get null when isnumeric returns 0.
SELECT CASE ISNUMERIC(QTY) WHEN 1 THEN CAST(QTY AS float) ELSE null END
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With