Solved

ISNUMERIC() equivalent?

Posted on 2001-09-13
5
6,830 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
To form a query 10 399
How do we check sybase license in ASE 1 2,836
SQL Left join on same table 4 303
Time optimization for insert/update in ultralite. 3 129
One of the biggest threats in the cyber realm pertains to advanced persistent threats (APTs). This paper is a compare and contrast of Russian and Chinese APT's.
Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

808 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