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,832 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Hot fix for .Net Crystal Reports 10.2.3600.0 to fix problems with sub reports running on 64 bit operating systems ISSUE: Reports which contain subreports fail with error "Missing Parameter Value" DEPLOYMENT SERVER OS: Windows 2008 with 64 bi…
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…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

910 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

16 Experts available now in Live!

Get 1:1 Help Now