Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a varchar is a number (TSQL)

is there an easy way to figure out if a varchar is a number?

Examples:

abc123 --> no number

123 --> yes, its a number

Thanks :)

like image 381
grady Avatar asked Jan 05 '11 10:01

grady


People also ask

How can I check if a varchar is numeric in SQL Server?

SQL Server ISNUMERIC() Function 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 check if a string has a number in SQL?

Check if a string ISNUMERIC()? There is an inbuilt function ISNUMERIC() in SQL Server which checks and returns TRUE if the string has “only digits” (so it is a number) otherwise FALSE.

Can a varchar be a number?

As the name suggests, varchar means character data that is varying. Also known as Variable Character, it is an indeterminate length string data type. It can hold numbers, letters and special characters.

How do you check if a field contains a character SQL?

Method 1 - Using CHARINDEX() function This function is used to search for a specific word or a substring in an overall string and returns its starting position of match. In case no word is found, then it will return 0 (zero).


2 Answers

ISNUMERIC will not do - it tells you that the string can be converted to any of the numeric types, which is almost always a pointless piece of information to know. For example, all of the following are numeric, according to ISNUMERIC:

£, $, 0d0

If you want to check for digits and only digits, a negative LIKE expression is what you want:

not Value like '%[^0-9]%' 
like image 86
Damien_The_Unbeliever Avatar answered Oct 25 '22 17:10

Damien_The_Unbeliever


ISNUMERIC will do

Check the NOTES section too in the article.

like image 42
Sachin Shanbhag Avatar answered Oct 25 '22 17:10

Sachin Shanbhag