Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

DB2 - SELECT only numbers from a column

Posted on 2014-04-22
10
Medium Priority
?
4,058 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 46

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
Google Certified Professional - Cloud Architect

This course (1 of 3) is designed to help students who are interested in Google Cloud Platform (GCP) to become familiar with the platform, navigate the console and learn its capabilities. It will also prepare students for the Google Cloud Architect certification exam.

 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 1000 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 46

Accepted Solution

by:
Kent Olsen earned 1000 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 46

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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

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…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

670 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