T-sql - determine if value is integer

后端 未结 20 2204
情深已故
情深已故 2020-11-30 12:11

I want to determine if a value is integer (like TryParse in .NET). Unfortunatelly ISNUMERIC does not fit me because I want to parse only integers a

20条回答
  •  醉酒成梦
    2020-11-30 12:39

    With sqlserver 2005 and later you can use regex-like character classes with LIKE operator. See here.

    To check if a string is a non-negative integer (it is a sequence of decimal digits) you can test that it doesn't contain other characters.

    SELECT numstr
      FROM table
     WHERE numstr NOT LIKE '%[^0-9]%'
    

    Note1: This will return empty strings too.

    Note2: Using LIKE '%[0-9]%' will return any string that contains at least a digit.

    See fiddle

提交回复
热议问题