Solved

Phone Number Format with extension in DB2

Posted on 2014-03-14
5
1,223 Views
Last Modified: 2014-03-17
Hi,

   We are on DB2 UDB 9.7.
  I want to write a SQL to format the phone number and extension to display the following format. My current format is : Phone Number: 5555555555 and extension in the format: 1234.
Please help.
(555) 555-5555 Ext. 1234

Open in new window

0
Comment
Question by:pvsbandi
[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
  • 2
  • 2
5 Comments
 
LVL 27

Expert Comment

by:tliotta
ID: 39930622
...to format the phone number and extension...

You showed us the presentation format that you want to see, but you didn't tell us anything about the data type(s?) and size(s?) of the column(s?) in your database. What is being formatted? What is the column definition? Is it numeric or character? How big?

Tom
0
 

Author Comment

by:pvsbandi
ID: 39935405
Sorry..here is the information.

   Two columns in my database: STAFF.Work_PHONE_TX is a varchar(10).
                                                   STAFF.Work_Phone_EXTN_TX is a varchar(5).

If STAFF.Work_PHONE_TX = '5555555555' and STAFF.Work_Phone_EXTN_TX = '1234', then i want the format to be displayed as (555) 555-5555 Ext. 1234
0
 
LVL 18

Accepted Solution

by:
Dave Ford earned 500 total points
ID: 39935422
This ought to do it:

select '(' ||
       substr(Work_PHONE_TX,1,3) ||
       ') ' ||
       substr(Work_PHONE_TX,4,3) ||
       '-' ||
       substr(Work_PHONE_TX,7,4) ||
       ' Ext. ' ||
       Work_Phone_EXTN_TX as formattedPhone                  
  from deleteme

Open in new window


HTH,
DaveSlash
0
 

Author Closing Comment

by:pvsbandi
ID: 39935532
Thank you! works as expected.
0
 
LVL 27

Expert Comment

by:tliotta
ID: 39935788
There is one minor potential problem. The STAFF.Work_PHONE_TX column is VARCHAR, so the column expects values that might have fewer than 10 characters. If it should be expected always to have 10 significant characters, the presentation possibly should ensure that the characters are always right-justified in a CHAR(10) intermediate field.

Tom
0

Featured Post

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

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…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

710 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