Help using REPLACE with wildcard matching pattern....

Hi all,

Can anyone help me with a little problem I can't solve. I am trying to update a phone_number column to replace any character that is not 0-9 with an empty string ie:



01234-567-890 (result i want = 01234567890)

012345 6789ext (result i want = 0123456789)

n/a (result i want = )

...12345..... (result i want = 12345)

i can identify which records have have non-numeric values in the phone number using...

select *
where phone_number like '%[^0-9]%'

but I want to update this table and remove non-numeric characters in the phone_number field something along the lines of....

set phone_number = replace(phone_number, [^0-9], '')

Any help would be greatly appreciated

Many thanks,


more ▼

asked Mar 30, 2010 at 11:39 AM in Default

Paul 3 gravatar image

Paul 3
21 1 1 1

(comments are locked)
10|1200 characters needed characters left

6 answers: sort newest

OK just for fun, here's a TSQL method, using Jeff Moden's Tally table, and a SQL2005 (and later) trick for concatenating the results back

--Jeff Moden Tally table
    if object_id('dbo.tally') is not null 
    drop table dbo.tally

    select top 10000 --change to fit max lenght of phone number
            identity(int,1,1) as n
       into dbo.tally
       from master.dbo.syscolumns sc1,
            master.dbo.syscolumns sc2

    -- add pk to maximize performance
      alter table dbo.tally
        add constraint pk_tally_n 
            primary key clustered (n) with fillfactor = 100

declare @phonetable table 
        (uniqueid int identity(1,1), phone_number varchar(500))
insert into @phonetable (phone_number)
        select '01234-567-890'
union   select '012345 6789ext' 
union   select 'n/a'
union   select '...12345.....'

;with cte (uniqueid, phone_number, goodchar, badchar) as(
    uniqueid, phone_number, 
    case when substring(phone_number,N,1) not like '%[^0-9]%' 
         then substring(phone_number,N,1) end as goodchar,
    case when substring(phone_number,N,1) like '%[^0-9]%' 
         then substring(phone_number,N,1) end as badchar

from @phonetable , Tally
where phone_number like '%[^0-9]%'
and N <= len(phone_number)

SELECT distinct
        stuff ( ( SELECT
                          '' + goodchar
                          cte t1
                  where t1.UniqueID = t2.UniqueID
        FOR XML PATH ( '' ) ) , 1 , 0 , '' )
        ,'') as clean_phone_number
from cte t2


phone_number clean_phone_number ...12345..... 12345 012345 6789ext 0123456789 01234-567-890 01234567890 n/a 
more ▼

answered Mar 30, 2010 at 01:40 PM

Kev Riley gravatar image

Kev Riley ♦♦
53.8k 47 49 76

+1 - Nice. I was trying to think how a tally table could get involved but didnt have enough time to sit down and work on it. Thanks.
Mar 31, 2010 at 04:43 AM Fatherjack ♦♦
Cheers, although it needs some serious tuning if it was to be used for real....
Mar 31, 2010 at 04:55 AM Kev Riley ♦♦
(comments are locked)
10|1200 characters needed characters left

Why not?

Select (Replace(Replace(phone_number, '-', ''), ' ', '') from phone_table

more ▼

answered Jun 06, 2012 at 05:04 PM

jacked gravatar image


That would only replace the '-' and ' ' characters. The original question was to replace all non-numerics.
Jun 06, 2012 at 10:22 PM Kev Riley ♦♦
(comments are locked)
10|1200 characters needed characters left

Kev and Mukesh both have very good answers you may want to consider. The other option you may want to consider is writing a CLR in C#/VB.NET that uses regex to do what you want and then you can run that against the table from within SQL Server.

more ▼

answered Apr 01, 2010 at 03:05 PM

TimothyAWiseman gravatar image

15.6k 20 23 32

(comments are locked)
10|1200 characters needed characters left

DECLARE @s VARCHAR(1000),  @i TINYINT,  @result VARCHAR(50) 

SELECT @s = '012345 6789ext'

SET @i = 1

SET @result = ''

WHILE (@i <= Datalength(@s)) BEGIN SET @result = @result + Substring(@s,Patindex('%[0-9]%',@s),1)

SET @s = Stuff(@s,Patindex('%[0-9]%',@s),1,'') 

SET @i = @i + 1 


SELECT @result
more ▼

answered Mar 31, 2010 at 06:37 AM

Srikant Maurya gravatar image

Srikant Maurya

U can Past above code in function and call function in select statment
Mar 31, 2010 at 06:38 AM Srikant Maurya
(comments are locked)
10|1200 characters needed characters left

CREATE FUNCTION ExtractInteger ( @String VARCHAR(2000) ) RETURNS VARCHAR(1000) AS BEGIN  DECLARE @Count INT  DECLARE @IntNumbers VARCHAR(1000)  SET @Count = 0  SET @IntNumbers = ''  WHILE @Count <= LEN(@String)  BEGIN  IF SUBSTRING(@String, @Count, 1) >= '0'  AND SUBSTRING(@String, @Count, 1) <= '9'  BEGIN  SET @IntNumbers = @IntNumbers + SUBSTRING(@String, @Count, 1)  END  SET @Count = @Count + 1  END 

RETURN @IntNumbers 


-- SELECT dbo.ExtractInteger('0323-111-CALL') -- gives 0323111
more ▼

answered Mar 30, 2010 at 04:26 PM

Mukesh gravatar image

37 1 1 2

(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

New code box

There's a new way to format code on the site - the red speech bubble logo will automatically format T-SQL for you. The original code box is still there for XML, etc. More details here.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.



asked: Mar 30, 2010 at 11:39 AM

Seen: 30932 times

Last Updated: Jun 06, 2012 at 10:22 PM