现在的位置: 首页 > 综合 > 正文

拆分字符串

2014年01月07日 ⁄ 综合 ⁄ 共 638字 ⁄ 字号 评论关闭

有字符串 a;b1;c23;d34  ,其中;為分割符。
現要實現功能:根據傳入的數值來刪除對應的項,如1則結果為 b1;c23;d34,如2則結果為a;c23;d34,如3則結果為a;b1;d34,如4則結果為a;b1;c23。

 

if object_id('f_str')is not null drop function f_str
go
create function f_str(@s varchar(50),@name int)
returns varchar(30)
as
begin
   
set @s=replace(@s,';','.')
   
set @s=case @name when 4 then replace(@s,'.'+parsename(@s,1),'')
                     
when 3 then replace(@s,parsename(@s,2)+'.','')
                     
when 2 then replace(@s,parsename(@s,3)+'.','')
                     
when 1 then replace(@s,parsename(@s,4)+'.' ,'')   
           
end
   
return replace(@s,'.',';')
end

go
-------------------------------------------
select s=dbo.f_str('a;b1;c23;d34',1)

【上篇】
【下篇】

抱歉!评论已关闭.