Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 989
Kings Avatar asked Aug 12 '13 13:08

Kings


People also ask

How do I find non numeric values in SQL?

This is the way: SELECT * FROM TABLE_NAME WHERE NOT REGEXP_LIKE(COLUMN_NAME, '^-?[0-9.]+$ '); This also excludes values which contain decimals.

How do I find non numeric characters in SQL?

SQL Server has an ISNUMERIC() function that returns 1 for numeric values and 0 for non-numeric values.

How do you check if a column value is numeric or not in SQL Server?

The ISNUMERIC() function tests whether an expression is numeric. This function returns 1 if the expression is numeric, otherwise it returns 0.

What is the use of Isnumeric in SQL?

As defined in the official Microsoft SQL Server documentation, the ISNUMERIC function determines whether an expression is a valid numeric type. It is a scalar function that takes a string expression as a parameter and returns an integer.


1 Answers

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 
like image 89
gbn Avatar answered Oct 21 '22 18:10

gbn