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

x
?
Solved

SQL 2008 R2 syntax

Posted on 2014-12-11
3
Medium Priority
?
191 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 600 total points
ID: 40495032
replace(yourfield, '/', '-')
0
 
LVL 70

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 1000 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 61

Assisted Solution

by:HainKurt
HainKurt earned 400 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

877 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