[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 7076
  • Last Modified:

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

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
Yossele
Asked:
Yossele
2 Solutions
 
momi_sabagCommented:
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
 
Dave FordSoftware Developer / Database AdministratorCommented:

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
 
Dave FordSoftware Developer / Database AdministratorCommented:

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Kent OlsenData Warehouse Architect / DBACommented:
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
 
Dave FordSoftware Developer / Database AdministratorCommented:

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
 
Kent OlsenData Warehouse Architect / DBACommented:

[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
 
mlmccCommented:
Any reason Crystal is inckuded in the zones?

mlmc
0
 
ghp7000Commented:
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now