【叶子函数分享二】去除字符串中连续的分割符

本文介绍了一个SQL自定义函数m_delrepeatsplit,该函数用于从输入字符串中移除连续出现的指定分隔符,并提供了一个示例演示如何使用此函数。需要注意的是,在处理大量数据时,使用自定义函数可能会对性能产生较大影响。

--创建函数

create function [dbo].[m_delrepeatsplit]

(

@str varchar(2000),

@split nvarchar(200)

)

returns nvarchar(2000)

as

begin

--begin declare

declare @count int,@i int,@isnull int

declare @newchar nvarchar(200),@nn nvarchar(300)

set @count=len(@str);set @i=1;set @isnull=1;set @nn='';

--end declare

--begin while

while @i<@count+1

begin

set @newchar=substring(@str,@i,1)

if(@isnull=1)

begin

set @nn=@nn+@newchar;

if(@newchar=@split)

begin

set @isnull=0;

end

else

begin

set @isnull=1;

end

end

else

begin

if(@newchar=@split)

begin

set @isnull=0;

end

else

begin

set @nn=@nn+@newchar;

set @isnull=1;

end

end

set @i=@i+1;

end

--end while

return @nn

end

--2、测试示例

declare @str nvarchar(200)

set @str='1 2 3 4 555 6 7 7';

declare @split nvarchar(200)

set @split=' ';

select dbo.m_delrepeatsplit(@str,@split) as newchar

--3、运行结果

/*

newchar

------------------

1 2 3 4 555 6 7 7

*/

特别说明:

如果数据量比较大,尽量避免使用自定义函数,以免严重影响性能。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值