Solved

ISNUMERIC() equivalent?

Posted on 2001-09-13
5
6,923 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Arrow Electronics was searching for a KVM  (Keyboard/Video/Mouse) switch that could display on one single monitor the current status of all units being tested on the rack.
Adults who share images on social media aren’t the only ones who need to worry about their privacy. Our culture’s tendency to share every move and celebration affects the privacy of our children, too.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

749 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