Solved

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

Posted on 2014-09-30
10
178 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: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
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

863 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

20 Experts available now in Live!

Get 1:1 Help Now