12

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
2

2 Answers 2

30

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 
Sign up to request clarification or add additional context in comments.

2 Comments

Your string lacks the ending '
Just for my own future reference: LIKE '%[^0-9]%' will consider blank as integer while NOT LIKE '%[0-9]%' will consider it as non integer.
1

There is also an ISNUMERIC function in MSSQL if you're using version 2008 or later.

mentioned link

IF (not ISNUMERIC(@rptID) = 1)
    THEN throw Error

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.