当前位置:首页 » 多媒体相关

一个取子串的问题


在我的一个表中,每条记录的同一个字段都保存如下值:  
  张三,李四,王小二(这个值可能会很长,每一个人名用逗号分开)  
  我现在要写一个触发器,当上面那个表改变时(准确的说是上表state这个列被改变时)我要取出张三,李四,王小二,然后把他们插到另一个表中形成三条记录。  
  那位可以帮我一下,怎么要sql中把字符串“张三,李四,王小二”拆成“张三”,“李四”,“王小二”三个字符串。  
   
  100分相送

推荐阅读

  • Windows 2003操作系统优化技巧 [详细内容]
  • 超强功能TomTom NAVIGATOR 6终抵达GPS地图软件及方案 [详细内容]
  • 只看热门的 手持车载GPS导航仪推荐GARMIN 任我游 610GPS [详细内容]
  • 【魔兽战术】不死面对暗夜基本技巧 [详细内容]
  • 隐藏游戏 片片 小说等等的几个方法 [详细内容]
  • 敢问路在何方 新科GPS推出多款产品车载GPS导航设备 [详细内容]
  • 保护(IIS)web服务器的15个技巧 [详细内容]
  • 网友回答:
    网友:txlicenhe

    declare   @aa   varchar(100)  
      set   @aa   =   张三,李四,王小二  
       
      while   charindex(,,@aa)   >   0  
      begin  
      print   left(@aa,charindex(,,@aa)-1)  
      set   @aa   =   right(@aa,len(@aa)   -   charindex(,,@aa))  
      end  
      print   @aa  
       
       
     

    网友:crazyfor

    参考:  
      --n要连继,  
      select   top   8000   identity(int,1,1)   as   n   into   numtab   from    
      (select   top   100   id=1     from   sysobjects)   as   a,  
      (select   top   100   id=1     from   sysobjects)   as   b,  
      (select   top   100   id=1     from   sysobjects)   as   c  
       
      ---------------------------------------------------------------------  
      declare   @a   table   (id   int,string   varchar(8000))  
      insert   @a   select   1   ,a,b,c,sd,dfsdfg  
      union   select     2,     a,n,sdf,we,t  
      union   select     3,     s,df,df  
       
       
       
      select   a.*,b.*,id,substring(,+string+,,n+1,charindex(,,,+string+,,n+1)-(n+1))  
      from   @a   a,numtab   b    
      where   substring(,+string+,,n,8000)   like   ,_%  
      order   by   id,n

    网友:friendliu

    create   table   name  
      (   name   varchar(10)  
      )  
      declare   @aa   varchar(100)  
      declare   @bb   varchar(10)  
      set   @aa   =   张三,李四,王小二  
      while   charindex(,,@aa)   >   0  
      begin  
      select   @bb=left(@aa,charindex(,,@aa)-1)  
      set   @aa   =   right(@aa,len(@aa)   -   charindex(,,@aa))  
                        insert   into   name   values(+@bb+)  
      end  
     

    .

    讨论区

    Login