Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server : How to test if a string has only digit characters

I am working in SQL Server 2008. I am trying to test whether a string (varchar) has only digit characters (0-9). I know that the IS_NUMERIC function can give spurious results. (My data can possibly have $ signs, which should not pass the test.) So, I'm avoiding that function.

I already have a test to see if a string has any non-digit characters, i.e.,

some_column LIKE '%[^0123456789]%' 

I would think that the only-digits test would be something similar, but I'm drawing a blank. Any ideas?

like image 383
skyline01 Avatar asked Feb 06 '15 16:02

skyline01


People also ask

How do I check if a string contains only numbers in SQL?

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

How do you find the number of characters in a string in SQL?

LEN() function calculates the number of characters of an input string, excluding the trailing spaces. It is an expression that can be a constant, variable, or column of either character or binary data. Returns : It returns the number of characters of an input string, excluding the trailing spaces.

How do I check if a string is an integer in SQL?

Syntax to check if the value is an integer. select yourColumnName from yourTableName where yourColumnName REGEXP '^-?[0-9]+$'; The query wherein we have used regular expression. This will output only the integer value.


1 Answers

Use Not Like

where some_column NOT LIKE '%[^0-9]%' 

Demo

declare @str varchar(50)='50'--'asdarew345'  select 1 where @str NOT LIKE '%[^0-9]%' 
like image 189
Pரதீப் Avatar answered Sep 19 '22 14:09

Pரதீப்