|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Wednesday, February 29, 2012 7:33 AM
Points: 19,
Visits: 60
|
|
I need some code to change the phone numbers in a field from one format to another.
Original number +44 (0) 1908 123 456 Changed number +44 (1908) 123 456
Any help is greatly appreciated.
|
|
|
|
|
SSCoach
         
Group: General Forum Members
Last Login: Yesterday @ 1:45 PM
Points: 15,442,
Visits: 9,572
|
|
Is the bit removed always a zero, or will it sometimes be something else?
- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC Property of The Thread
"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Wednesday, February 29, 2012 7:33 AM
Points: 19,
Visits: 60
|
|
Always a zero
Thanks for the quick reply
|
|
|
|
|
SSCoach
         
Group: General Forum Members
Last Login: Yesterday @ 1:45 PM
Points: 15,442,
Visits: 9,572
|
|
Try something like this:
declare @Phone varchar(100);
select @Phone = '+44 (0) 1908 123 456';
select stuff(stuff(replace(@Phone, '(0) ', ''), 5, 0, '('), 10, 0, ')');
- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC Property of The Thread
"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
|
|
|
|
|
SSC-Dedicated
           
Group: Administrators
Last Login: Today @ 3:19 PM
Points: 31,526,
Visits: 13,863
|
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Wednesday, February 29, 2012 7:33 AM
Points: 19,
Visits: 60
|
|
Thanks for that it does what I want for the example phone number.
I also have other phone numbers though they might be +44 (0) 1908 123 456 or +44 (0) 121 430 4992
The only constants are the first 7 chars the rest are fluid.
+44^(0)^XXX^YYYYYYY
XXX could be 3 or 4 or 5 chars and YYYYYYY is the remainder of the number.
When I get a moment I will have a play to see if I can get something to work just am a bit busy at the moment to spend a lot of time on it.
Again thanks for your help
|
|
|
|
|
SSCoach
         
Group: General Forum Members
Last Login: Yesterday @ 1:45 PM
Points: 15,442,
Visits: 9,572
|
|
After you remove the "(0)", does the second set of numbers always get parens around it? If so, then Steve's suggestion of using charindex/patindex to find the spaces around that and add parens to them will work.
- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC Property of The Thread
"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Wednesday, February 29, 2012 7:33 AM
Points: 19,
Visits: 60
|
|
| Yes I think that will be the way as I am basically trying to get the area code into the brackets (removing the 0) leaving the rest of the phone number untouched.
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 5:19 PM
Points: 33,110,
Visits: 27,035
|
|
Think not of what to do to the row... think of what to do to the column... and, you don't have to do it all at once... Divide'n'Conquer. ;)
--===== Create and populate a test table. This is NOT part of the solution DECLARE @PhoneNumbers TABLE (Original VARCHAR(30)) INSERT INTO @PhoneNumbers (Original) SELECT '+44 (0) 1908 123 456' UNION ALL SELECT '+44 (0) 121 430 4992'
SELECT Original, REPLACE(STUFF(PartialFormat,CHARINDEX(' ',PartialFormat),0,')'),'(',' (') AS Reformatted FROM (--==== Partially reformat the phone number by replacing the (0) and surrounding spaces -- with just a left parentheses... SELECT Original, REPLACE(Original, ' (0) ','(') AS PartialFormat FROM @PhoneNumbers ) d
Yields... Original Reformatted -------------------- ------------------ +44 (0) 1908 123 456 +44 (1908) 123 456 +44 (0) 121 430 4992 +44 (121) 430 4992
--Jeff Moden "RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".
First step towards the paradigm shift of writing Set Based code: Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."
For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
|
|
|
|
|
SSC-Dedicated
           
Group: Administrators
Last Login: Today @ 3:19 PM
Points: 31,526,
Visits: 13,863
|
|
|
|
|