Solved

DB2 - SELECT only numbers from a column

Posted on 2014-04-22
10
3,234 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
  • 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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:daveslash
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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

November 2009 Recently, a question came up in the DB2 forum regarding the date format in DB2 UDB for AS/400.  Apparently in UDB LUW (Linux/Unix/Windows), the date format is a system-wide setting, and is not controlled at the session level.  I'm n…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

820 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