现有表名为aaa
地区     内容
----------------
中国     021sp.html|管材|4355;028sp.html|建筑材料|3209
中国     023sp.html|材|4356;025sp.html|建|9209需实现效果:
地区     内容
----------------
中国     021sp.html|管材|4355
中国     028sp.html|建筑材料|3209
中国     023sp.html|材|4356
中国     025sp.html|建|9209请赐教,谢谢。

解决方案 »

  1.   

    if object_id('[aaa]') is not null drop table [aaa]
    go
    create table [aaa]([地区] varchar(4),[内容] varchar(45))
    insert [aaa]
    select '中国','021sp.html|管材|4355;028sp.html|建筑材料|3209' union all
    select '中国','023sp.html|材|4356;025sp.html|建|9209'
    goselect  a.地区,
     内容=substring(a.内容,b.number,charindex(';',a.内容+';',b.number)-b.number)
    from [aaa] a
    join master..spt_values b on b.type='P'
    where charindex(';',';'+a.内容,b.number)=b.number/**
    地区   内容
    ---- ---------------------------------------------
    中国   021sp.html|管材|4355
    中国   028sp.html|建筑材料|3209
    中国   023sp.html|材|4356
    中国   025sp.html|建|9209(4 行受影响)
    **/
      

  2.   

    -->根据树哥的换个条件
    if object_id('[aaa]') is not null drop table [aaa]
    go
    create table [aaa]([地区] varchar(4),[内容] varchar(45))
    insert [aaa]
    select '中国','021sp.html|管材|4355;028sp.html|建筑材料|3209' union all
    select '中国','023sp.html|材|4356;025sp.html|建|9209'
    go-->开始查询
    select  a.地区,
            内容=substring(a.内容,b.number,charindex(';',a.内容+';',b.number)-b.number)
    from [tb] a
    join master..spt_values b on b.type='P'
    where substring(';'+a.内容,b.number,1)=';'-->测试结果
    ---- --------------------------
    /*
    地区 内容
    中国 021sp.html|管材|4355
    中国 028sp.html|建筑材料|3209
    中国 023sp.html|材|4356
    中国 025sp.html|建|9209
    */
      

  3.   


    参考 http://topic.csdn.net/u/20080612/22/c850499f-bce3-4877-82d5-af2357857872.html?76208
      

  4.   

    -->测试数据(注意此处为了方便我缔造了一个id列)
    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb](id int,[地区] varchar(4),[内容] varchar(45))
    insert into  [tb]
    select '1','中国','021sp.html|管材|4355'     union all
    select '1','中国','028sp.html|建筑材料|3209' union all
    select '2','中国','023sp.html|材|4356'       union all
    select '2','中国','025sp.html|建|9209'
    go
    -->开始查询
    select 地区,
           内容=stuff((select ';'+内容 from tb where id=a.id for xml path('')),1,1,'')
    from tb a
    group by id,地区-->测试结果
    ----- ---------------------------------------------
    /*
    地区 内容
    中国 021sp.html|管材|4355;028sp.html|建筑材料|3209
    中国 023sp.html|材|4356;025sp.html|建|9209
    */