Solved

SQL 2008 R2 syntax

Posted on 2014-12-11
3
167 Views
Last Modified: 2014-12-11
How do I change phone number records in SQL table
Most of current phone numbers in the DB written in like
XXX/XXX-XXXX format  401/396-2400
I would like to change it to
 XXX-XXX-XXXX format 401-396-2400
I am looking for an exact syntax to replace all ‘/’ with ‘-‘  for SQL 2008 R2 db
0
Comment
Question by:leop1212
3 Comments
 
LVL 11

Accepted Solution

by:
John_Vidmar earned 150 total points
ID: 40495032
replace(yourfield, '/', '-')
0
 
LVL 69

Assisted Solution

by:ScottPletcher
ScottPletcher earned 250 total points
ID: 40495033
Easy to replace:
--UPDATE ...
--SET phone_number =
REPLACE(phone_number, '/', '-')
--WHERE phone_number LIKE '%/%'

But better would be to remove all formatting in the value stored in the db and add it back when needed when reading from the table.
0
 
LVL 51

Assisted Solution

by:HainKurt
HainKurt earned 100 total points
ID: 40495105
just get rid of all non numeric values and format when you need it...

update table set phone = replace(replace(phone,'/',''),'-','')

then, modify your app and do not allow any other character, or use 3 fields, and concat & save to db, or use some custom control that allows formatted input...

when you need it, you can always format it like:

select left(phone,3) + '-' + substring(phone, 4,3) + '-' + right(phone,4) as formatted_phone
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

920 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now