Solved

Writing a SQL query to consolidate 2 fileds into the same format

Posted on 2014-09-30
10
184 Views
Last Modified: 2014-10-13
I have a table users which consists 2 column phoneNumber and Contact (there more columns into that table userID).

PhoneNUmber is an old Field (in the diffferent format xxx-xxx-xxxx,xxx.xxx.xxxx,(xxx)xxx-xxxx).Wjhat I want to do is to take that field and format it into this format (xxx-xxx-xxxx) and put it into the column Contact
0
Comment
Question by:erikTsomik
[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
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 21

Expert Comment

by:Tapan Pattanaik
ID: 40352760
Hi erikTsomik,

If you want to retrieve "xxx-xxx-xxxx" from   "xxx-xxx-xxxx,xxx.xxx.xxxx,(xxx)xxx-xxxx)" you can use the below query.

select left(phoneNumber,12) from users ;
0
 
LVL 19

Author Comment

by:erikTsomik
ID: 40352770
But that will give a mixture of numbers .What I need to is to take the phoneNumber  column and update to format xxx-xxx-xxxx and put it into the Contact column
0
 
LVL 21

Expert Comment

by:Tapan Pattanaik
ID: 40352780
can you share more data with us !!!!!!
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40353240
You'll just end up with different formats again!

Strip the formatting from the stored data.  Formatting chars are not actual data in this case.

Add a formatting code, if you need it, to determine how to format the data when it is output/displayed.

C'mon, wouldn't format: xxxxxxxxxx be a lot simpler and more consistent all the way around!?
0
 
LVL 19

Author Comment

by:erikTsomik
ID: 40353253
How would I strip it off can you give me an example
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40353264
You could use a function that stripped every nonnumeric char.

Or, if your data is reasonably clean, just embed REPLACEs:

UPDATE dbo.tablename
SET contact = REPLACE(REPLACE(REPLACE(REPLACE(phone_number, '-', ''), '.', ''), '(', ''), ')') --...
WHERE
    contact IS NULL OR contact = ''


Then add a CHECK to the table to prevent bad data from getting in:

ALTER TABLE table_name
ADD CONSTRAINT table_name__CK_contact CHECK(contact NOT LIKE '%[^0-9]%')
0
 
LVL 19

Author Comment

by:erikTsomik
ID: 40353305
will constraint remove strip off unwanted data
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40353320
No, it will prevent non-numeric char(s) from being INSERTed into the contact column.
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40353326
For example:


create table table_name ( contact varchar(30) not null )

ALTER TABLE table_name
 ADD CONSTRAINT table_name__CK_contact CHECK(contact NOT LIKE '%[^0-9]%')
 go
 
 insert into table_name values('111')
 insert into table_name values('12345678990898098')
 insert into table_name values('1234b')
 go
 select * from table_name


If you wanted, you could also add a check to verify that the length is either 7 or 10 digits.
0
 
LVL 32

Accepted Solution

by:
awking00 earned 500 total points
ID: 40355007
>>PhoneNUmber is an old Field (in the diffferent format xxx-xxx-xxxx,xxx.xxx.xxxx,(xxx)xxx-xxxx).<<
Do all PhoneNumber records contain 10 numeric characters, but with different formats using periods, dashes, spaces and/or parentheses? If so -
update table set contact =
substring(replace(replace(replace(replace(replace(phonenumber,'-',''),'.',''),'(',''),')',''),' ',''),1,3) + '-' +
substring(replace(replace(replace(replace(replace(phonenumber,'-',''),'.',''),'(',''),')',''),' ',''),4,3) + '-' +
substring(replace(replace(replace(replace(replace(phonenumber,'-',''),'.',''),'(',''),')',''),' ',''),7,4);
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Suggested Solutions

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

734 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