Solved

how to format phone number in sql server

Posted on 2012-04-09
4
284 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 500 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

773 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