Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Reformat Phone Numbers

Author  Topic 

Dan1978
Starting Member

5 Posts

Posted - 2012-09-11 : 09:25:07

Hi,

I am trying to create a process to reformat telephone numbers in a table. Below are the main types of errors I'm faced with e.g. letters at the end of the numbers, spaces in middle, mobile numbers missing a leading zero. Would anyone be able to provide some information about the best way to resolve/program the below issue.

All help would be much appreciated.

Examples of what is in the database and what I would like the output to be.

01473 743719 Ray ==> 01474743719
01226386486 – clt ==> 01226386486
07763 650906 ==> 07763650906
7894728202 ==> 07894728202

xhostx
Constraint Violating Yak Guru

277 Posts

Posted - 2012-09-11 : 09:40:50
[code]declare @var varchar(40)
set @var='78947 28202 lkj'
SELECT CASE WHEN SUBSTRING(replace(substring (@var,1, PATINDEX('%[A-Za-z]%',@var)-1),CHAR(32),''),1,1)like '0' THEN replace(substring (@var,1, PATINDEX('%[A-Za-z]%',@var)-1),CHAR(32),'')
when SUBSTRING (replace(substring (@var,1, PATINDEX('%[A-Za-z]%',@var)-1),CHAR(32),''),1,1) like '%[1-9]%' then '0'+replace(substring (@var,1, PATINDEX('%[A-Za-z]%',@var)-1),CHAR(32),'')
end[/code]

this case has all exception.

--------------------------
Joins are what RDBMS's do for a living
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-09-11 : 11:01:19
[code]
SELECT LEFT(REPLACE(field,' ',''),CASE WHEN PATINDEX('%[^0-9]%',REPLACE(field,' ',''))=0 THEN LEN(REPLACE(field,' ','')) ELSE PATINDEX('%[^0-9]%',REPLACE(field,' ',''))-1 END)
FROM table
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Dan1978
Starting Member

5 Posts

Posted - 2012-09-11 : 11:15:01
Thank you for your help
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-09-11 : 11:28:00
wc

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -