Has anyone seen this SQL bug before?
If you use the IsNumeric function with 12345 and replace number 3 with an "e" and no other letter, SQL still thinks it's numeric.
if ISNUMERIC('12e45') = 1print 'Is Numeric'
else
Print 'No'
Technology Tips and News
Has anyone seen this SQL bug before?
If you use the IsNumeric function with 12345 and replace number 3 with an "e" and no other letter, SQL still thinks it's numeric.
if ISNUMERIC('12e45') = 1That isn't a bug, it's just how that function works (yes you could argue this makes it less than useful in a lot of scenarios)
Consider
select cast('12e45' as float)
Hi,
In addition to disssss's response: It is important to understand that ISNUMERIC does not check if this is number! but if this value can be CONVERTED to numeric. or in other word: Determines whether an expression is a valid numeric type. Therefore, (1) there are several types of numerics like decimal, float, real, int, bigint, smallint, tinyint, money, smallmoney... some of these have several formats that we can use (2) even if the value is not number but can be converted to numeric according the internal set of implicit convert rules, then it will ret
ISNUMERIC isn't particularly useful for the common use case of checking for a string containing only the digits 0 through 9. Consider a CASE expression instead to get the desired behavior:
CASE WHEN @value LIKE '%[^0-9]%' OR @value = '' THEN 0 ELSE 1 END
In addition to other posts, on SQL2012 or later, you can use try_convert to check if the number is convertible to a specific data type:
SELECT CASE WHEN try_convert(int, @str) IS NOT NULL
THEN 'Converts'
ELSE 'Not convertible'
END
Hi BerzinP,
As per the
BOL:ISNUMERIC, ISNUMERIC returns 1 for some characters that are not numbers, such as plus (+), minus (-), and valid currency symbols such as the dollar sign ($). For a complete list of currency symbols, see
money and smallmoney (Transact-SQL).
SELECT ISNUMERIC('123') as '123' ,ISNUMERIC('abc') as 'abc' ,ISNUMERIC('-') as '-' ,ISNUMERIC('+') as '+' ,ISNUMERIC('$') as '$' ,ISNUMERIC('.') as '.' ,ISNUMERIC(',') as ',' ,ISNUMERIC('\') as '\'
See a blog Use new TRY_PARSE() instead of ISNUMERIC() | SQL Server 2012