Solved

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

Posted on 2014-09-30
10
176 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
 
LVL 69

Expert Comment

by:ScottPletcher
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 69

Expert Comment

by:ScottPletcher
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:ScottPletcher
ID: 40353320
No, it will prevent non-numeric char(s) from being INSERTed into the contact column.
0
 
LVL 69

Expert Comment

by:ScottPletcher
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 31

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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

Suggested Solutions

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 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