Solved

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

Posted on 2014-09-30
10
182 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
  • 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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 Backup & Restore 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.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

685 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