SQL Server : How to test if a string has only digit characters

后端 未结 7 1578
误落风尘
误落风尘 2021-02-05 00:59

I am working in SQL Server 2008. I am trying to test whether a string (varchar) has only digit characters (0-9). I know that the IS_NUMERIC function can give spurious results.

相关标签:
7条回答
  • 2021-02-05 01:31

    There is a system function called ISNUMERIC for SQL 2008 and up. An example:

    SELECT myCol
    FROM mTable
    WHERE ISNUMERIC(myCol)<> 1;
    

    I did a couple of quick tests and also looked further into the docs:

    ISNUMERIC returns 1 when the input expression evaluates to a valid numeric data type; otherwise it returns 0.

    Which means it is fairly predictable for example

    -9879210433 would pass but 987921-0433 does not. $9879210433 would pass but 9879210$433 does not.

    So using this information you can weed out based on the list of valid currency symbols and + & - characters.

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