Bug with ISNUMERIC()

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') = 1
print 'Is Numeric'
else 
Print 'No'

July 7th, 2015 9:06pm

That 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)

https://msdn.microsoft.com/en-us/library/ms186272.aspx?f=255&MSPPError=-2147217396
Free Windows Admin Tool Kit Click here and download it now
July 7th, 2015 10:06pm

The use of E or e for floating point numbers started in the 1950's with FORTRAN and later in Algol. You wrote "twelve times ten to the forty-fifth power" in the standard notation. The only difference is that some languages have to start with a digit and some can start with the E. 
July 7th, 2015 10:31pm

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

Free Windows Admin Tool Kit Click here and download it now
July 7th, 2015 10:49pm

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

July 7th, 2015 11:24pm

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

Free Windows Admin Tool Kit Click here and download it now
July 8th, 2015 6:34am

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

If you have any question, feel free to let me know.

July 9th, 2015 3:07am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics