Solved

SQL 2008 R2 syntax

Posted on 2014-12-11
3
168 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:Scott Pletcher
Scott Pletcher 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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

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…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

816 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

12 Experts available now in Live!

Get 1:1 Help Now