当前位置: 代码迷 >> Sql Server >> 求一字符串截取sql语句,
  详细解决方案

求一字符串截取sql语句,

热度:82   发布时间:2016-04-27 14:00:39.0
求一字符串截取sql语句,在线等。
一个字段内容为:
SQL code
A,B,C,D,E,F,J,H,I,J,K,L,M,N,O,P,Q,

输出结果为:
SQL code
A,B,C,<A>D,E,F,<A>J,H,I,<A>M,N,O,<A>P,Q,

能被3整除的逗号后面加个<A>。

------解决方案--------------------
SQL code
declare @a varchar(100),@b varchar(100),@i intselect @a='A,B,C,D,E,F,J,H,I,J,K,L,M,N,O,P,Q,',@b='',@i=0while @i<len(@a)/6begin     set @[email protected]+substring(@a,@i*6+1,6)+'<A>'    set @[email protected]+1endselect @[email protected]+substring(@a,len(@a)/6*6,len(@a)-len(@a)/6*6)select @b/*----------------------------------------------------------------A,B,C,<A>D,E,F,<A>J,H,I,<A>J,K,L,<A>M,N,O,<A>,P,Q(1 行受影响)*/
------解决方案--------------------
SQL code
declare @a varchar(100),@b varchar(100),@c varchar(100),@i intselect @a='A,B,C,D,E,F,J,H,I,J,K,L,M,N,O,P,Q,',@b='',@i=1,@[email protected]while @i<=len(@a)-len(replace(@a,',',''))begin    if(@i%3=0)        set @b = @b + substring(@c,1,charindex(',',@c)-1) + ',<A>'    else        set @b = @b + substring(@c,1,charindex(',',@c)-1) + ','    set @c = substring(@c,charindex(',',@c)+1,len(@c)-charindex(',',@c))    set @[email protected]+1endselect @b
  相关解决方案