Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


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

Posted on 2008-10-16
Medium Priority
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.?

Question by:Yossele
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
LVL 37

Accepted Solution

momi_sabag earned 75 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 ....
where rtrim(ltrim(translate(your_string,' ','1234567890'))) = ''
LVL 18

Assisted Solution

by:Dave Ford
Dave Ford earned 75 total points
ID: 22731285

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


    inString VARCHAR(100)
  RETURNS SmallInt
  modifies sql data
-- Scratch variables used for processing
DECLARE stringLength INT;
DECLARE loopCounter INT;
-- If the incoming string is NULL, return 0
IF (inString IS NULL) THEN
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;
-- Return the final output

Open in new window

LVL 18

Expert Comment

by:Dave Ford
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.

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

-- DaveSlash
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 46

Expert Comment

by:Kent Olsen
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.

LVL 18

Expert Comment

by:Dave Ford
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.

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

-- DaveSlash
LVL 46

Expert Comment

by:Kent Olsen
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.   :)


LVL 101

Expert Comment

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

LVL 13

Expert Comment

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.

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

There have always been a lot of questions related to when Crystal Reports evaluates report components (such as formulas, summaries, cross-tabs, charts, to name a few examples). Crystal Reports uses a two-pass reporting process to provide greater …
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

609 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