Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

DB2 - SELECT only numbers from a column

Posted on 2014-04-22
10
Medium Priority
?
4,351 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 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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

783 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