Solved

How do I test if a column defined as non-numeric has numeric data in DB2-SQL?

Posted on 2008-10-16
8
4,999 Views
Last Modified: 2008-10-25
I am using SQL for DB2 V8.
I want to test a column for numeric value, even that the column is of non-numeric type, in a SELECT statement.
Is there a function that does it.?

Thanks
Yossele
0
Comment
Question by:Yossele
8 Comments
 
LVL 37

Accepted Solution

by:
momi_sabag earned 25 total points
ID: 22731122
you can use the translate function and translate each numeric character to blank,
then use trim and check if you get the empty string
for example

select ....
from
where rtrim(ltrim(translate(your_string,' ','1234567890'))) = ''
0
 
LVL 18

Assisted Solution

by:daveslash
daveslash earned 25 total points
ID: 22731285

There are several ways to accomplish this, but I use the following user-defined function:

HTH,
DaveSlash

CREATE FUNCTION IsNumeric (
    inString VARCHAR(100)
)
  RETURNS SmallInt
  LANGUAGE SQL
  modifies sql data
 
BEGIN
-- Scratch variables used for processing
DECLARE stringLength INT;
DECLARE loopCounter INT;
DECLARE charAtPos VARCHAR(1);
 
-- If the incoming string is NULL, return 0
IF (inString IS NULL) THEN
 RETURN 0;
End If;
 
-- Initialize the scratch variables
SET stringLength = LENGTH(inString);
SET loopCounter = 1;
 
-- Loop over the string
WHILE (loopCounter <= stringLength) DO
   -- Get the single character off the string
   SET charAtPos = LOWER(SUBSTRING (inString, loopCounter, 1));
 
   IF ((CharAtPos < '0') or (CharAtPos > '9')) then
      RETURN 0;
   END IF;
 
   SET loopCounter = loopCounter + 1;
END WHILE;
 
-- Return the final output
RETURN 1;
END

Open in new window

0
 
LVL 18

Expert Comment

by:daveslash
ID: 22731416

If you wanted to account for decimals and negative-signs, that's easy enough to add to either of the two methods above.

e.g.
select .....
from  .....
where trim(translate(avc,' ','.-1234567890')) = ' '

-- DaveSlash
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 45

Expert Comment

by:Kdo
ID: 22731485
I'm with Momi on this one.  Why write a UDF when straight-forward SQL will suffice?

In his example, the calls to RTRIM and LTRIM are unnecessary.  If the underlying data type is VARCHAR, the returned value is be an empty string.  If the underlying data type is CHAR, the value returned by the calls to TRANSLATE, RTRIM, and/or LTRIM will be padded to match the size of the source variable.


Kent
0
 
LVL 18

Expert Comment

by:daveslash
ID: 22731513

One last thing. If you have spaces that you consider non-numeric, then you'll need ro translate that space into a character value before comparing it.

e.g.
where  trim(translate(avc,'            z','.-1234567890 ')) = ' '

-- DaveSlash
0
 
LVL 45

Expert Comment

by:Kdo
ID: 22731754

[slightly off topic]

It seems that an awful lot of data questions could be answered with a relatively straight-forward "IsRegex" function to test user data against a user supplied format string.

 IsRegex ('[0-9]*', your_string)
 IsRegex ('[0-9]{1,*}', your_string)   -- Don't allow an empty string

It wouldn't be much more than encapsulating the regular expression evaluator in all *nix distributions into a function.  Because of it's recursive nature, I'd hate to write one in SQL, but writing it in C++ is nearly trivial.

Maybe I'll suggest this to IBM.   :)

[/slightly]

Kkent
0
 
LVL 100

Expert Comment

by:mlmcc
ID: 22733271
Any reason Crystal is inckuded in the zones?

mlmc
0
 
LVL 13

Expert Comment

by:ghp7000
ID: 22753043
I prefer the lazy approach, if possible. meaning it depends on the number of rows that have to be evaluated and what the action taken will be if indeed there is numeric value there. So, I quickly export the column to a del file and then import into MS access, defining the column as int. Access will generate an import error file listing the line number that contains the error! I know,  lazy and cumbersome, but it works!
If the number of rows is quite large, then C++ utility is what I use.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

785 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