Solved

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

Posted on 2008-10-16
8
5,189 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
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 
LVL 45

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.


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: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.   :)

[/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

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Union 2 queries to a cte (temp table perhaps) 9 41
SQL Server 2012 r2 - Sum totals 2 28
Sum not calculating correctly 22 39
Supress a section of a field 6 19
Crystal Reports: 5 Tests for Top Performance It is complete, your masterpiece report.  Not only does it meet your customer’s expectations, it blows them out the water, all they want is beautifully summarised and displayed in a myriad of ways. …
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

839 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