Why does ISNUMERIC('.') return 1?

后端 未结 3 406
逝去的感伤
逝去的感伤 2020-11-28 14:35

Recently I was working with ISNUMERIC in SQL Server, when I encountered a problem, which led to finding this snippet of code.

SELECT ISNUMERIC(\'.\')
         


        
相关标签:
3条回答
  • 2020-11-28 14:58

    Because "." is used in a decimal number !

    see here

    isnumeric for '-' & '.' Why isnumeric('-') & isnumeric('.') returning 1?

    Answer: Because "-" means negative and "." is used in a decimal number. I have no clue why they named it ISNUMERIC though. They should have named it, ISNUMBERRELATED.

    0 讨论(0)
  • 2020-11-28 15:07

    See IsNumeric() Broken? Only up to a point.

    SELECT CAST('.' AS MONEY) 
    

    returns 0.00 (though the cast fails for int and float)

    ISNUMERIC just checks that the value can be cast to any one of the numeric datatypes which is generally useless. Usually you want to know whether it can be cast to a specific type.

    Additionally it doesn't even seem to do that task correctly for all possible inputs.. ISNUMERIC(' ') returns 0 despite casting successfully to both int and money. Conversely ISNUMERIC(N'8') returns 1 but does not cast successfully to anything that I tried.

    Some useful helper functions for that are here IsNumeric, IsInt, IsNumber.

    SQL Server 2012 introduced TRY_PARSE and TRY_CONVERT that help with this greatly.

    0 讨论(0)
  • 2020-11-28 15:20

    I think it also interprets a number of other non-numeric fields as numeric, there's further info here -

    http://classicasp.aspfaq.com/general/what-is-wrong-with-isnumeric.html

    0 讨论(0)
提交回复
热议问题