Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get if a string is a number in T-SQL

Is there an easy way to get if string is an integer number (consists only of digits) in MS SQL 2005?

Thank you for your help.

like image 865
StuffHappens Avatar asked Jan 21 '10 13:01

StuffHappens


2 Answers

Even though the original poster was referring to SQL 2005, I found in 2008 r2 a straight where isnumeric(string) resulted in an error 4145 non-boolean type. To resolve this use:where isnumeric(string) = 1

like image 60
Craig Avatar answered Sep 22 '22 22:09

Craig


The function ISNUMERIC returns whether a string is numeric, but will return true for non-integers.

So you could use:

WHERE ISNUMERIC(str) AND str NOT LIKE '%.%' AND str NOT LIKE '%e%' AND str NOT LIKE '%-%'
like image 40
Paul Avatar answered Sep 22 '22 22:09

Paul