?
Solved

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

Posted on 2014-09-30
10
Medium Priority
?
198 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
Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

 
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 2000 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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

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.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

764 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