Search code examples
sqlsql-server-2000

Check if a variable contains any non-numeric digits in SQL Server


I've a query like below:-

DECLARE @rptID VARCHAR(8)
SET @rptID = (SELECT reportID FROM Reports)

In general @rptID contains numeric digits like '00001234' etc. But is there any way to validate if the variable @rptID contains any non-numeric value in it.

For ex.

IF (@rptID contains non-numeric value)
            THEN throw Error

Solution

  • Check for any characters that are not in the range 0 to 9

    ^ is not in LIKE expressions

    IF @rptID LIKE '%[^0-9]%'
       --throw error