假设现在一个表中有三个字段,表结构如下:    A         |    B         C        
813zx-01-5/9  |
813zx-01-5/12 |我如何通过一个统一的语句将字段A中的两条记录中的字符5截取出来放到B中,第一条记录中的最后的数字 9 和第二条记录中的 12 截取出来放到字段C中。谢谢!!

解决方案 »

  1.   

    update tablename set b=substring(A,10,1)
      

  2.   

    declare @T table(A varchar(20),B varchar(20),C varchar(20))
    insert into @t(A) select '813zx-01-5/9'
    union all select '813zx-01-5/12' update @t set b=Substring(A,charindex('/',A)-1,1),c=Substring(A,charindex('/',A)+1,len(A)) from @t
    select * from @t
      

  3.   

    update tablename set c=right(a,len(a)-charindex('/',a))
      

  4.   

    update tablename set b=substring(A,10,1),c=right(a,len(a)-charindex('/',a))
      

  5.   

    declare @T table(A varchar(20),B varchar(20),C varchar(20))
    insert into @t(A) select '813zx-01-5/9'
    union all select '813zx-01-5/12' 
    union all select '813zx-01-51/12' 
    union all select '813zx-01-53331/31312'update @t set b=left(right(A,charindex('-',reverse(A))-1),charindex('/',right(A,charindex('-',reverse(A))-1))-1),
                  c=Substring(right(A,charindex('-',reverse(A))-1),charindex('/',right(A,charindex('-',reverse(A))-1))+1,len(right(A,charindex('-',reverse(A))-1)))select * from @t
      

  6.   

    update 表名稱 set b=Substring(A,charindex('/',A)-1,1),c=Substring(A,charindex('/',A)+1,len(A))對上面語句的解釋:對于B字段,使用charindex來查找'/'的位數,然後使用substring函數根據這個位置取出一個字符
    注意: 此處使用的是charindex('/',A)-1對于C字段的處理基本上一樣,不過其取出的位數=不定,有可能1位,有可能2位,這時可以用len(A)來處理,如果數值太大,SUBSTRING函數最多會取至末尾