Home  >  Article  >  Database  >  MSSQL 字符段分段更新

MSSQL 字符段分段更新

WBOY
WBOYOriginal
2016-06-07 18:04:301122browse

MSSQL 字符段分段更新,大家可以参考下。

-->分段更新
-->Author:wufeng4552
-->Date :2009-10-07 08:13:41
--如:更新'a,a,a,a,a' 第三段a 為 'test' 結果為'a,a,a,test,a'
declare @s varchar(20)--更新的字符串
declare @split varchar(10)--分割符
declare @splitlen int
declare @pos int --更新的段 如上為第三段
declare @value varchar(10) --更新後的值 'test'
declare @I int,@J int --變量
select @s='a,a,a,a,a',@split=',',@splitlen=len(@split+'a')-2,@i=1,@j=charindex(@split,@s+@split),@pos=3,@value='test'
--循環開始
while @pos>0 and @Ibegin
select @pos=@pos-1,@I=@J+@splitlen+1,@J=charindex(@split,@s+@split,@i)
end
select @s=case when @Iwhen @J>len(@s) then @s+@value
when @I=@J then stuff(@s,@I,0,@value)
else @s end
select @s
/*
--------------------
a,a,a,test,a
(1 個資料列受到影響)
*/
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn