Solved

DB2 - SELECT only numbers from a column

Posted on 2014-04-22
10
2,875 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 142

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:Kdo
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
 
LVL 142

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:
Kdo 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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 (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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now