SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Remove area code from telephone number
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

ChazH
Starting Member

Cayman Islands
4 Posts

Posted - 02/23/2013 :  13:29:28  Show Profile  Reply with Quote

Hi All,

New to SQL Server and new to the forum!

How would I go about removing the area code from a customer table?

The column is nvarchar(30) numbers are stored as (345)111-1111 and I want to reformat to 1111111.

Thanks in advance!

James K
Flowing Fount of Yak Knowledge

3323 Posts

Posted - 02/23/2013 :  17:56:30  Show Profile  Reply with Quote
REPLACE(STUFF(PhoneNumberColumn,1,CHARINDEX(')',PhoneNumberColumn),''),'-','')
Go to Top of Page

ChazH
Starting Member

Cayman Islands
4 Posts

Posted - 02/23/2013 :  22:30:14  Show Profile  Reply with Quote
Thanks I will try it out!
Go to Top of Page

ChazH
Starting Member

Cayman Islands
4 Posts

Posted - 02/25/2013 :  16:59:18  Show Profile  Reply with Quote
Thanks again for your help but I was not able to figure out the format for this command.

The table is dbo.customer and the column name is customer.phonenumber. I tried a bunch of different things but could not get it to work on my test database.
Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3323 Posts

Posted - 02/25/2013 :  17:22:54  Show Profile  Reply with Quote
If you want to keep the data in the table as it is and remove the area code in a select, do this:

SELECT REPLACE(STUFF(phonenumber,1,CHARINDEX(')',phonenumber),''),'-','') FROM dbo.customer
If you want to permanently change the table by updating it to remove the area code, do this:
UPDATE dbo.customer SET
   phonenumber = REPLACE(STUFF(phonenumber,1,CHARINDEX(')',phonenumber),''),'-','');
Go to Top of Page

ChazH
Starting Member

Cayman Islands
4 Posts

Posted - 02/25/2013 :  18:25:05  Show Profile  Reply with Quote
Thanks! I will learn the syntax tonight and try it out on my test server tomorrow.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000