Solved

DB2 - SELECT only numbers from a column

Posted on 2014-04-22
10
3,458 Views
Last Modified: 2014-04-29
I have a Phone number fied (PHONE_NBR) in TBL_PHONE.  The values can be
123-456-7890
1234567890
123 456 7890
(123) 456-7890
+91234567890

Is there a way that I can get just the numbers 1234567890 in a SELECT statement.

Thanks,
0
Comment
Question by:ttta83
[X]
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
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40014899
you can achieve that with an expression like this:
SELECT PHONE_NBR
  from TBL_PHONE
WHERE LENGTH(RTRIM(TRANSLATE(PHONE_NBR, '*', ' 0123456789'))) = 0 

Open in new window

0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 40014903
Use the DIGITS function.

SELECT digits (phone_number) FROM mytable...


Kent
0
 

Author Comment

by:ttta83
ID: 40015006
Guy Hengel,

I have over 20,000 rows but

WHERE LENGTH(RTRIM(TRANSLATE(PHONE_NBR, '*', ' 0123456789'))) = 0  

only returns 30 rows.

----------------------------------

KDO,

phone_number is defined as CHAR(15) so DIGITS(phone_number) doesn't work.


Thanks!
0
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 250 total points
ID: 40015039
CHAR(15) means you will have trailing spaces...

hence, my suggestion will return those that have 15 digits phone numbers...
will this work then for you?
WHERE LENGTH(RTRIM(TRANSLATE( RTRIM(PHONE_NBR), '*', ' 0123456789'))) = 0
0
 
LVL 45

Accepted Solution

by:
Kent Olsen earned 250 total points
ID: 40015095
Using Guy's approach, if you know that the unwanted characters are predictable, you can use something like this:

  select replace (translate ('(123)456-7890', '', '()-+'), ' ', '') from sysibm.sysdummy1

If that doesn't cover all of the cases, add the remaining offending characters to the string '()-+'.
0
 

Author Comment

by:ttta83
ID: 40015502
Is there a built-in function in DB2 to select just the numerics?  There are a lot of junks data in the phone number column so it's hard to find all non-numeric chars to put in the replace string.

Thanks!
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 40018222
Hi ttta,

No.  But it's pretty easy to find the offending characters

  SELECT phonecolumn, translate (phonecolumn, ' ', '0123456789()-+')
  FROM mytable
  WHERE translate (phonecolumn, ' ', '0123456789()-+') <> ' '
  FETCH FIRST 1 ROWS ONLY;

That will find the first row with an invalid character.  Add the character(s) to the string in the query above and run it again.

You can also start by adding the suspect characters.  Alphabetics, special characters, etc.
0
 

Author Comment

by:ttta83
ID: 40018262
ok, thanks a lot for your help!
0
 
LVL 18

Expert Comment

by:Dave Ford
ID: 40029990
I realize this question is already answered, but I've been on vacation for over a week, so let me give you a user-defined function I wrote many years ago to do exactly what you're asking about.

CREATE FUNCTION IntegerPart (
  inString VARCHAR(100)
)
  RETURNS integer
  LANGUAGE SQL
  modifies sql data
  set option dbgview = *source

BEGIN
-- Scratch variables used for processing
DECLARE outputString VARCHAR(100);
DECLARE stringLength INT;
DECLARE loopCounter INT;
DECLARE charAtPos VARCHAR(1);

-- If the incoming string is NULL, return NULL
IF (inString IS NULL) THEN
 return NULL;
End If;
-- Initialize the scratch variables
SET outputString = '';
SET stringLength = LENGTH(inString);
SET loopCounter = 1;

-- Loop over the string
WHILE (loopCounter <= stringLength) DO
   -- Get the single character off the string
   SET charAtPos = SUBSTRING (inString, loopCounter, 1);

   -- If the character is numeric, add it to the result
   IF ((CharAtPos >= '0') and (CharAtPos <= '9')) then
      SET outputString = outputString || charAtPos;
   END IF;

   SET loopCounter = loopCounter + 1;
END WHILE;

-- Return the final output
if (outputString = '') then
   RETURN NULL;
end if;

RETURN (integer(outputString));

END

select IntegerPart('a1b2c3!@#$89')
  from sysibm.sysdummy1

INTEGERPART
     12,389

Open in new window


HTH,
DaveSlash
0
 

Author Comment

by:ttta83
ID: 40030110
Thank you!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

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…
Recursive SQL in UDB/LUW (it really isn't that hard to do) Recursive SQL is most often used to convert columns to rows or rows to columns.  A previous article described the process of converting rows to columns.  This article will build off of th…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

752 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