I am dealing with a legacy database that is all kinds of messed up with its datatypes. There are columns that should be integers
, but are saved as varchar(9)
. I am creating a new version of the table and trying to import the integer values properly. I have the following statement for the fields that need to be converted to int:
CASE
ISNUMERIC(fieldname)
WHEN 0 THEN NULL
ELSE CAST(fieldname AS int) AS fieldname
The value that keeps tripping this up is: 029999E00
.
The error is:
Conversion failed when converting the varchar value '029999E00' to type int.
Most of the time, this works great and will give me a valid int value or NULL.
Why is the ISNUMERIC
saying this value is int, but then CAST
is failing to convert it? I have tried bigint
also and I get the same result. Is there another way to check for values like this?
There is another post about using ISNUMERIC as the solution, but ISNUMERIC doesn't work in this case. I need another option to check for a numeric value or to make sure the CAST statement won't fail when it gets the value if it's a Hex value.