Solved

# ISNUMERIC() equivalent?

Posted on 2001-09-13
Medium Priority
1,157 Views
Don't ask me why, but a colleague has a need for the equivalent of MS SQL Server's ISNUMERIC() function in Transact SQL. Is there a stored proc somewhere already that can accomplish the feat pretty quickly?

I'd search PAQ, but it doesn't seem to be working!
0
Question by:dwalex

LVL 5

Expert Comment

ID: 6481657
I have written the following SP for sybase to check if a given string is Numeric. It returns 0 if numeric else -1.

You can modify it to have print statements also.

Thanks.

create proc isNumber @value varchar(15)
as
begin
declare @number_count int
declare @decimal_count int
select  @decimal_count = 0
select  @number_count = char_length(@value)
while (@number_count > 0)
begin
if(substring(@value,@number_count,1)
IN ('0','1','2','3','4','5','6','7','8','9','.'))
begin
select @number_count = @number_count - 1
if(substring(@value,@number_count,1) = '.')
begin
select @decimal_count = @decimal_count + 1
if(@decimal_count > 1)

return -1
end
end
else return -1
end
return 0
end
0

LVL 3

Accepted Solution

mathavra earned 400 total points
ID: 6488444
Here is another method I have used

select patindex('%[^0-9]%', "123R23")
go
------------
4

will return non-zero value if any of character is non-numeric. Else, it will return zero.

select patindex('%[^0-9]%', "123023")
go
------------
0

You can use this function anywhere to check whether it returns zero or non-zero.

Hope this helps!

Mathav
0

LVL 10

Expert Comment

ID: 6488861
Neither of these techniques work for negative numbers, or for numbers in scientific notation (-3.456e5)

-bret
0

LVL 1

Author Comment

ID: 6489244
That's the kind of quick answer I was after. As bret says, it's not perfect, but for a financial application, it helps alot.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I am posting this in case anyone runs into similar issues that I did, this may save you a lot of grief: Condition: 1. Your NetBIOS domain name contains an ampersand " & " character.  (e.g. AT&T) 2. You've tried to run any Microsoft installation…
Welcome back to our beginners guide of the popular Unix tool, cron. If you missed part one where we introduced this tool, the link is below. We left off learning how to build a simple script to schedule automatic back ups. Now, we’ll learn how to se…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
###### Suggested Courses
Course of the Month8 days, 17 hours left to enroll