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,608 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
 
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Recursive SQL in UDB/LUW (you can use 'recursive' and 'SQL' in the same sentence) A growing number of database queries lend themselves to recursive solutions.  It's not always easy to spot when recursion is called for, especially for people una…
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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

707 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now