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

I have a request as shown below: -

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

Usually @rptID contains numeric digits such as "00001234", etc. But is there a way to check if the @rptID variable contains any non-numeric value in it.

For instance,

 IF (@rptID contains non-numeric value) THEN throw Error 
+5
source share
2 answers

Check for characters that are not between 0 and 9

^ does not apply to LIKE expressions

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

MSSQL also has an ISNUMERIC function if you are using version 2008 or later.

mentioned link

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

Source: https://habr.com/ru/post/1496598/


All Articles