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

x
?
Solved

how to format phone number in sql server

Posted on 2012-04-09
4
Medium Priority
?
294 Views
Last Modified: 2012-04-09
My database has phone numbers, some that are already formatted correctly and some that are null.

I need a command that I can run on the server to updated these numbers so they are all in this format  xxx-xxx-xxxx.

Not sure how to weed out the ones already in that format and the nulls. And also verify there are 10 digits before applying format.

I'm using sql server 2005

Thanks.
0
Comment
Question by:rutledgj
  • 2
  • 2
4 Comments
 
LVL 23

Expert Comment

by:wdosanjos
ID: 37823840
Are the values to be formatted all numbers already?  Or they come in a variety of different formats (for example, (xxx) xxx-xxxx)?
0
 

Author Comment

by:rutledgj
ID: 37823881
All are numbers but some already have the dashes there. These don't need to be changed.
0
 

Author Comment

by:rutledgj
ID: 37823882
Some entries are null
0
 
LVL 23

Accepted Solution

by:
wdosanjos earned 2000 total points
ID: 37823899
Please try:
update TheTable
set phone = substring(phone,1,3) + '-' + substring(phone,4,3) + '-' + substring(phone,8,4)
where phone not like '___-___-____' and len(phone) = 10

Open in new window

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

824 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