Fifteen Things I Hate About ISNUMERIC


Thanks for reading!

Brent says: the funny part to me is that if you just try to union all of them, SQL Server throws its hands up:


COME ON SQL SERVER YOU JUST TOLD ME THEY WERE ALL NUMERIC, dammit, convert to numeric for me if you’re so smart.

Previous Post
Computed Columns and Cardinality Estimates
Next Post

17 Comments. Leave new

  • ISNUMERIC sucks, but TRY_CONVERT is awesome 🙂

  • Hah, I have nearly the same demo that use to freak out the Devs when I spot them trying isnumeric!

    I also ran Try_Convert with all the options to show them what a mess that is compared to ISNumeric. Enjoy guessing what works and why! Or sanitize yer darn data in .NET rather than asking SQL to do it!

    DECLARE @Numbs TABLE([Test] VARCHAR(25), [Label] VARCHAR(25) NOT NULL, [Set] VARCHAR(25))
    INSERT INTO @Numbs values (‘.’,’Period’,’Chars’),(‘,’,’Comma’,’Chars’),(‘-‘,’Minus’,’Chars’),(‘+’,’Plus’,’Chars’)
    ,(‘ ‘,’Tab’,’Chars’),(‘ ‘,’Space’,’Chars’),(NULL,’SQLNULL’,’Chars’)
    ,(‘-0′,’NegZero’,’Mixed’),(‘- 0′,’NegSpaceZero’,’Mixed’),(‘-0e-0′,’NegENeg’,’Mixed’)

    select [Set], [Label], [Test], CAST([Test] AS VARBINARY(50)) AS [Hex], ISNUMERIC([Test]) AS [Is?]
    , TRY_CONVERT(tinyint,[Test]) AS [TINYINT], TRY_CONVERT(smallint,[Test]) AS [SMALLINT], TRY_CONVERT(int,[Test]) AS [INT]
    , TRY_CONVERT(bigint,[Test]) AS [BIGINT], TRY_CONVERT(decimal,[Test]) AS [DECIMAL], TRY_CONVERT(numeric,[Test]) AS [NUMERIC]
    , TRY_CONVERT(float,[Test]) AS [FLOAT], TRY_CONVERT(real,[Test]) AS [REAL]
    , TRY_CONVERT(smallmoney,[Test]) AS [SMALLMONEY], TRY_CONVERT(money,[Test]) AS [MONEY]
    FROM @Numbs
    ORDER BY [Set], [Label]

    • I was going to post just the int,decimal,float version of this and challenge people to guess which ones worked. But seeing your extended counter example list ; wtf on the leading comma vs non-leading comma on the comma separated ints….. anyone who scores more than about 5 points right has spent too much time doing hand coded sql ….

    • Henrik Staun Poulsen
      February 28, 2018 1:39 am

      hi Mark,
      Thank you very much for a horrible example of isnumeric and TryConvert. I was not aware that try_convert was that bad.

    • That’s a great demo! What’s the bottom line that you give your devs?
      P.S. CHAR(10) is line feed, not backspace

  • @Brent ascii() returns an int, although converting to numeric will still generate an error.

    • @Brent, I also beleive you were a bit fast on the trigger there, because the example with ISNUMERIC uses “CHAR(9)” where as the next example uses “ASCII(9)”, so they aren’t comparable 🙂
      Anyway love your Posts, keep em comming 😉

      • Richard — that’s my fault, I updated the code after drafting the post.

        Sorry about that!

  • I still have a few clients on 2008 so when testing for positive integers I use [SometimesAnInt] NOT LIKE ‘%[^0-9]%’

  • I stumbled upon this ( a while back regarding issues with ISNUMERIC and a WIP solution…


  • Granger Godbold
    March 1, 2018 12:16 pm

    On that note, I still find it a critical oversight that one cannot reliably convert a string directly to a decimal when it’s in scientific notation. Forcing people to go through a float means the number can be corrupted. E.g. SELECT CONVERT(decimal(30, 15), CONVERT(float, ‘0.543e+2’)) —> 54.299999999999997

    • Erik Darling
      March 1, 2018 12:20 pm

      Granger — yep, I used to run into this problem a lot when importing Excel files. Never found a more reliable way.

  • […] Out comes ISNUMERIC and all its failings. […]

  • […] de SQL Server 2005 aquí y la búsqueda para «ISNUMERIC’ en la página). Ver comentarios aquí en el Brent Ozar del gran blog. No vuelvas a usarlo para cualquier propósito. […]