下面只是部分内容:
ID     NAME
1      张;王;𞈡&#23
2      赵;钱;孙;񀕶
3      李𾣪&#986532
4      韩
   .
   .
   .
转换后
ID    NAME1    NAME2    NAME3
1      张       王
2      赵       钱       孙
3      李
4      韩
求高人指点!~如果单纯的按照“;”分割字符,最后还要处理“李&#256234”这样的字段,能不能一次性处理完啊?
菜鸟求解!~~~~~~~~~~~~~~~~~~~~~~~

解决方案 »

  1.   

    這個,,,寫函數一個一個判斷了,,,
    而且必須保證你的例子中已經存在所有可能出現的情況,,,
    然後就一個一個搜索了,,
    建議別在SQL裏做,,,重續裏去一個一個判斷容易寫得多
    這個要在SQL裏面作只有做
    xml動態拼接才有辦法,,,好難搞,,,,哪位有愛的大大來寫
      

  2.   

    說錯,,是程序裏去實現這個,容易得多,,就是個 insert 查詢拼接問題
    SQL裏面做就是,,,xml語句拼接了,,痛苦,,
      

  3.   

    ----------------------------------------------------------------
    -- Author  :SQL77(只为思齐老)
    -- Date    :2010-01-12 10:30:31
    -- Version:
    --      Microsoft SQL Server  2000 - 8.00.194 (Intel X86) 
    -- Aug  6 2000 00:57:48 
    -- Copyright (c) 1988-2000 Microsoft Corporation
    -- Desktop Engine on Windows NT 5.1 (Build 2600: Service Pack 3)
    --
    ----------------------------------------------------------------
    --> 测试数据:[TB]
    if object_id('[TB]') is not null drop table [TB]
    go 
    create table [TB]([ID] int,[NAME] varchar(19))
    insert [TB]
    select 1,'张;王;𞈡&#23' union all
    select 2,'赵;钱;孙;񀕶' union all
    select 3,'李𾣪&#986532' union all
    select 4,'韩'
    --------------开始查询--------------------------select ID,
    LEFT(SUBSTRING(T.NAME,NUMBER,CHARINDEX(';',T.NAME+';',NUMBER)-NUMBER),1)AS NAME
     from [TB] T,MASTER..SPT_VALUES WHERE TYPE='P' 
    AND SUBSTRING(';'+T.NAME,NUMBER,1)=';' 
    AND PATINDEX('%[吖-做]%',
    SUBSTRING(T.NAME,NUMBER,CHARINDEX(';',T.NAME+';',NUMBER)-NUMBER))>0
    ----------------结果----------------------------
    /* (所影响的行数为 4 行)ID          NAME 
    ----------- ---- 
    1           张
    1           王
    2           赵
    2           钱
    2           孙
    3           李
    4           韩(所影响的行数为 7 行)
    */那个又要行转列,感觉麻烦
      

  4.   

    77 要是在“韩”的前面加个“&#254689” 就是“&#254689韩”要怎么处理,刚本想修改一下问题的??帮帮忙再处理下哈!~
      

  5.   


    --要去除的是不是就是 & ,# ,0,1,2,3,4,5,6,7,8,9 这十二个字符?
    --如果是的话,就写一个function将这些字符都去掉再处理
    --感觉资料好混乱啊
    create function dbo.fn_replace(@name nvarchar(100))
    returns nvarchar(100)
    AS
    begin
    declare @re nvarchar(100)
        declare @table table(col nchar(01),name nvarchar(100))
        insert into @table 
          select '#',@name
    union select '&',@name
    union select '0',@name
    union select '1',@name
    union select '2',@name
    union select '3',@name
    union select '4',@name
    union select '5',@name
    union select '6',@name
    union select '7',@name
    union select '8',@name
    union select '9',@name
      
       Update @table
       set   @name=isnull(@re,name),
     @re=replace(@name,col,''),
         name=@re
     
      select @re=name from @table where col='9'  return @reEnd
      

  6.   

    [code=SQL]--> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    create table [tb]([ID] int,[NAME] varchar(19))
    insert [tb]
    select 1,'张;王;𞈡&#23' union all
    select 2,'赵;钱;孙;񀕶' union all
    select 3,'李𾣪&#986532' union all
    select 4,'韩'
    --函数
    create function dbo.getitemname(@name varchar(50),@pos int)
    returns varchar(30)
    as
    begin
    declare @ret varchar(30)
    declare @table table(id int identity(1,1),name varchar(30))
    insert into @table
    select substring(@name,number,charindex(';',@name+';',number)-number)
    from master..spt_values 
    where type='p' 
    and substring(';'+@name,number,1)=';'
    select @ret=name from @table where id = @pos
    if isnull(@ret,'')!=''
    begin
    select @ret=case patindex('%[吖-做]%',@ret) when 0 then null
    else case patindex('%[^吖-做]%',@ret) when 0 then @ret else 
    substring(@ret,patindex('%[吖-做]%',@ret),patindex('%[^吖-做]%',@ret)-1) end
    end
    end
    return @ret
    end--查询
    select ID, 
    dbo.getitemname([NAME],1) as [NAME1],
    dbo.getitemname([NAME],2) as [NAME2],
    dbo.getitemname([NAME],3) as [NAME3]
    from [tb]
    --结果
    ------------------------------
    1 张 王 NULL
    2 赵 钱 孙
    3 李 NULL NULL
    4 韩 NULL NULLcode]
      

  7.   

    ]--> 测试数据:[tb] 
    if object_id('[tb]') is not null drop table [tb] 
    create table [tb]([ID] int,[NAME] varchar(19)) 
    insert [tb] 
    select 1,'张;王;𞈡&#23' union all 
    select 2,'赵;钱;孙;񀕶' union all 
    select 3,'李𾣪&#986532' union all 
    select 4,'韩' 
    --函数 
    create function dbo.getitemname(@name varchar(50),@pos int) 
    returns varchar(30) 
    as 
    begin 
    declare @ret varchar(30) 
    declare @table table(id int identity(1,1),name varchar(30)) 
    insert into @table 
    select substring(@name,number,charindex(';',@name+';',number)-number) 
    from master..spt_values 
    where type='p' 
    and substring(';'+@name,number,1)=';' 
    select @ret=name from @table where id = @pos 
    if isnull(@ret,'')!='' 
    begin 
    select @ret=case patindex('%[吖-做]%',@ret) when 0 then null 
    else case patindex('%[^吖-做]%',@ret) when 0 then @ret else 
    substring(@ret,patindex('%[吖-做]%',@ret),patindex('%[^吖-做]%',@ret)-1) end 
    end 
    end 
    return @ret 
    end --查询 
    select ID, 
    dbo.getitemname([NAME],1) as [NAME1], 
    dbo.getitemname([NAME],2) as [NAME2], 
    dbo.getitemname([NAME],3) as [NAME3] 
    from [tb] 
    --结果 
    ------------------------------ 
    1 张 王 NULL 
    2 赵 钱 孙 
    3 李 NULL NULL 
    4 韩 NULL NULL 
      

  8.   


    --有个小问题
    --patindex('%[吖-做]%',@ret) 判断中文字符的起始位置
    create function dbo.getitemname(@name varchar(50),@pos int)
    returns varchar(30)
    as
    begin
    declare @ret varchar(30)
    declare @table table(id int identity(1,1),name varchar(30)) insert into @table
    select substring(@name,number,charindex(';',@name+';',number)-number)
    from master..spt_values 
    where type='p' 
    and substring(';'+@name,number,1)=';' select @ret=name from @table where id = @pos if isnull(@ret,'')!=''
    begin
    select @ret=stuff(@ret,1,patindex('%[吖-做]%',@ret)-1,'')--加上这句
    select @ret=case patindex('%[吖-做]%',@ret) when 0 then null
    else case patindex('%[^吖-做]%',@ret) when 0 then @ret else 
    substring(@ret,patindex('%[吖-做]%',@ret),patindex('%[^吖-做]%',@ret)-1) end
    end
    end
    return @ret
    end
      

  9.   

    my god ,你们也太强大了!