Solved

Phone Number Format with extension in DB2

Posted on 2014-03-14
5
1,192 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
  • 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:
daveslash 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

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…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

792 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