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

后端 未结 7 1601
误落风尘
误落风尘 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:20

    Method that will work. The way it is used above will not work.

    declare @str varchar(50)='79136'
    
    select 
      case 
        when  @str LIKE replicate('[0-9]',LEN(@str)) then 1 
        else 0 
      end
    
    declare @str2 varchar(50)='79D136'
    
    select 
      case 
        when  @str2 LIKE replicate('[0-9]',LEN(@str)) then 1 
        else 0 
      end
    

提交回复
热议问题