Technical Article

Remove Pattern Matched Characters

,

The first function recursively removes all characters matching a supplied patindex patten on a string by string basis. 

select cusno,last_name,[dbo].[xRemoveMatchedCharacters](last_name,'%[^0-9a-z]%')

 from cusmas c

select cusno,last_name,[dbo].[xRemoveMatchedCharacters](last_name,'%[0-9a-z]%')

 from cusmas c

The second version is a table based function built on the first function.

select cusno,last_name,z.*

 from cusmas c

 cross apply [dbo].[zRemoveMatchedCharacters](last_name,'%[^0-9a-z]%')z

select cusno,last_name,z.*

 from cusmas c

 cross apply [dbo].[zRemoveMatchedCharacters](last_name,'%[0-9a-z]%')z

CREATE FUNCTION [dbo].[xRemoveMatchedCharacters]
(
   @raw nvarchar(max), 
   @pattern nvarchar(max)
)
returns nvarchar(max)
as
begin
declare @Index int
set @Index = patindex(@pattern, @raw)
if @Index = 0
return @raw;

return dbo.xRemoveMatchedCharacters(replace(@raw,substring(@raw,@Index,1),''),@pattern);
end
go


CREATE FUNCTION [dbo].[zRemoveMatchedCharacters]
(
   @raw nvarchar(max), @pattern nvarchar(max)
)
   returns table
as
return
select dbo.xRemoveMatchedCharacters (@raw, @pattern)[clean]
go

Rate

2.25 (4)

You rated this post out of 5. Change rating

Share

Share

Rate

2.25 (4)

You rated this post out of 5. Change rating