Solved

ISNUMERIC() equivalent?

Posted on 2001-09-13
5
6,869 Views
Last Modified: 2008-02-26
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
Comment
Question by:dwalex
5 Comments
 
LVL 5

Accepted Solution

by:
amitpagarwal earned 100 total points
ID: 6481656
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 10

Expert Comment

by:bret
ID: 6482806
Sorry for the unconstructive criticism, but this procedure...

doesn't seem to handle negative numbers  "-1.0"
doesn't seem to handle numbers in scientific notation "2.345e20" (positive or negative)

I'm not aware of anything better, though (unless one uses the server-side Java option in ASE 12.0 and 12.5).

-bret
0
 
LVL 5

Expert Comment

by:amitpagarwal
ID: 6482809
ya bret,

i agree with u .. i should include this in my code ..

thanks a ton ..

0
 
LVL 1

Author Comment

by:dwalex
ID: 6485326
It's sufficient for my purposes, amitpagarwal, thanks. I'll add the negative sign, and may have to add parentheses too, and maybe commas? This is for a financial application, and sometimes these things appear too, I'll wager.

I was hoping there was a somewhat more elegant way than the brute force method, but it seems unlikely. Anyone with a different answer can earn another hundred points though.
0
 
LVL 4

Expert Comment

by:gardmanIT
ID: 20849642
Just for info and based on the correct answer above
Here is the function convderted to AS400 SQL

I also changed it to return 1 if numeric and 0 if not as this suited me better.

Cheers,

CREATE FUNCTION libraryname.isNumeric (@value varchar (15)) RETURNS INTEGER LANGUAGE SQL 
 
BEGIN 
 
 declare @number_count int
;
 
 declare @decimal_count int;
 
 set  @decimal_count = 0
;
 
 set  @number_count = char_length(@value)
;
 
  while (@number_count > 0)
 
 DO
 
    if(substring(@value,@number_count,1)
 IN ('0','1','2','3','4','5','6','7','8','9','.'))
 Then
 
      set @number_count = @number_count - 1
;
 
      if(substring(@value,@number_count,1) = '.')
 Then
 
        set @decimal_count = @decimal_count + 1
;
 
        if(@decimal_count > 1)
 Then
 
 
 
         return 0
;
 
       end
 
 if;
 
     end if
;
 
   else return 0
;
 
   end if;
 
  end while;
 
 return 1;
 
end
;

Open in new window

0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sybase SQL Syntax 2 285
SQL Query Syntax 11 166
sybase optimizer statistics 2 50
SQL, Updating Statistics take too long for 60GB database 5 162
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
The advancement in technology has been a great source of betterment and empowerment for the human race, Nevertheless, this is not to say that technology doesn’t have any problems. We are bombarded with constant distractions, whether as an overload o…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question