求一字符串截取sql语句,在线等。
一个字段内容为:
A,B,C,D,E,F,J,H,I,J,K,L,M,N,O,P,Q,
A,B,C,<A>D,E,F,<A>J,H,I,<A>M,N,O,<A>P,Q,
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 @b=@b+substring(@a,@i*6+1,6)+'<A>' set @i=@i+1endselect @b=@b+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 行受影响)*/
[解决办法]
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,@c=@awhile @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 @i=@i+1endselect @b