日期:2014-05-18  浏览次数:20624 次

求一SQL语句,还是字符串截取的问题,望高手们指点,在线等。
SQL code

--建表:
CREATE TABLE [dbo].[tb](
    [AB] [nvarchar](2000) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
--插入数据
insert into tb(AB)values('张三,李四,王五')
insert into tb(AB)values('张三,李四,王五,赵柳,王八')
insert into tb(AB)values('张无忌,张三丰,小名,赵刚,黎明,小凡,赵本山,范围')
insert into tb(AB)values('小名,赵刚,黎明,小凡,小李')
insert into tb(AB)values('ABD,ABD,ABD,ABD,ABD,ABD,ABD,ABD,ABD,ABD,ABD,ABD,ABD,ABD,ABD')


查询之后得到的格式如下:
SQL code

张三,李四,王五
张三,李四,王五,<|>赵柳,王八
张无忌,张三丰,小名,<|>赵刚,黎明,小凡,<|>赵本山,范围
小名,赵刚,黎明,<|>小凡,小李
ABD,ABD,ABD,<|>ABD,ABD,ABD,<|>ABD,ABD,ABD,<|>ABD,ABD,ABD,<|>ABD,ABD,ABD


也就是查找逗号 第三个逗号的后面追加一个“<|>”的符号,注意的是要保留逗号,望高手指点指点。

------解决方案--------------------
SQL code
with cte as
(select m.num,value=case when num1%4=0 then '<|>'+m.value else m.value end from(
select row_number()over(partition by num order by getdate()) num1,* from(
select a.num,value=substring(A.ab,b.number,CHARINDEX(',',a.ab+',',b.number)-b.number) from
(select ROW_NUMBER()over( order by getdate()) num,AB from #tb )a
 join master..spt_values b on type='p' and number<=len(a.ab) 
 where b.number=CHARINDEX(',',','+a.AB,b.number)) b)m
 )
select replace(STUFF((select ','+value from cte where num=a.num for XML path('')),1,1,''),'&lt;|&gt;','<|>') from cte a group by num

------解决方案--------------------
SQL code
CREATE TABLE [dbo].[tb](
    [AB] [nvarchar](2000)
) ON [PRIMARY]
--插入数据
insert into tb(AB)values('张三,李四,王五')
insert into tb(AB)values('张三,李四,王五,赵柳,王八')
insert into tb(AB)values('张无忌,张三丰,小名,赵刚,黎明,小凡,赵本山,范围')
insert into tb(AB)values('小名,赵刚,黎明,小凡,小李')
insert into tb(AB)values('ABD,ABD,ABD,ABD,ABD,ABD,ABD,ABD,ABD,ABD,ABD,ABD,ABD,ABD,ABD')
go

create function dbo.f_str(@ab varchar(2000)) returns varchar(2000)
as
begin
  declare @str varchar(2000)
  declare @i as int 
  declare @j as int 
  set @j = len(@ab) - len(replace(@ab,',',''))
  if @j < 3
     set @str = @ab
  else
    begin
      set @i = 1
      set @str = ''
      while @i <= @j
      begin
        set @str = @str + substring(@ab,1,charindex(',',@ab))
        if @i % 3 = 0
           set @str = @str + '<|>'
        set @ab = substring(@ab, charindex(',',@ab) + 1 , len(@ab))
        set @i = @i + 1
      end
      set @str = @str + @ab
    end
  return @str
end
go

select ab ,dbo.f_str(ab) newab From tb

drop table tb
drop function dbo.f_str

/*
ab                                                                                                                                                                                                                                                               newab                                                                                                                                                                                                                                                            
---------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------------------- 
张三,李四,王五                                                                                                                                                                                                                                                         张三,李四,王五
张三,李四,王五,赵柳,王八