Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

how to format phone number in sql server

Posted on 2012-04-09
4
Medium Priority
?
289 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
[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
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

670 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