--够麻烦的,看看charindex,substring
create table tb(ID varchar(10),NUM varchar(20))
insert into tb values('213', '213-AK25-JP91')
insert into tb values('213', '213-BK25-JP91')
insert into tb values('213', '213-CK25-JP91')
goselect id , num = left(num,charindex('-',num)-1) + '-' +
   case left(substring(num , charindex('-',num) + 1 , charindex('-' , num , charindex('-' , num) + 1) - charindex('-' , num) - 1),1) 
        when 'A' then 'G' 
        when 'B' then 'H' 
        when 'C' then 'Z' 
   end + 
   substring(substring(num , charindex('-',num) + 1 , charindex('-' , num , charindex('-' , num) + 1) - charindex('-' , num) - 1) , 2, 1) + 
   cast(cast(substring(substring(num , charindex('-',num) + 1 , charindex('-' , num , charindex('-' , num) + 1) - charindex('-' , num) - 1) , 3, 2) as int) + 1 as varchar) + '-' +
   reverse(left(reverse(num),charindex('-',reverse(num)) - 1))
from tbdrop table tb/*
id         num
---------- -------------
213        213-GK26-JP91
213        213-HK26-JP91
213        213-ZK26-JP91(3 行受影响)
*/

解决方案 »

  1.   

    declare @t table(ID char(3),NUM varchar(20))
    insert @t select '213','213-BK25-JP91 'declare @tb table(col1 char(1),col2 char(1))
    insert @tb select 'A','G'
    insert @tb select 'B','H'       
    insert @tb select 'C','Z'update a 
    set num = left(num,4)+(select col2 from @tb where col1 = substring(a.num,5,1))+substring(num,6,1)
    +right('00'+ltrim(cast(substring(num,7,2) as int )+1),2)+substring(num,9,5)
    from @t a
    select * from @t
    /*
    ID   NUM                  
    ---- -------------------- 
    213  213-HK26-JP91(所影响的行数为 1 行)
    */
      

  2.   

    --如果所有位置都是固定的.create table tb(ID varchar(10),NUM varchar(20))
    insert into tb values('213', '213-AK25-JP91')
    insert into tb values('213', '213-BK25-JP91')
    insert into tb values('213', '213-CK25-JP91')
    goselect id , num = left(num,4) + 
      case substring(num,5,1) when 'A' then 'G' when 'B' then 'H' when 'C' then 'Z' end +
      substring(num,6,1) +
      cast(cast(substring(num,7,2) as int) + 1 as varchar) + 
      substring(num,9,5)
    from tbdrop table tb
    /*
    id         num
    ---------- ---------------------------------------------------
    213        213-GK26-JP91
    213        213-HK26-JP91
    213        213-ZK26-JP91(3 行受影响)
    */
      

  3.   

    declare @t table(ID char(3),NUM varchar(20))
    insert @t select '213','213-BK25-JP91 'declare @tb table(col1 char(1),col2 char(1))
    insert @tb select 'A','G'
    insert @tb select 'B','H'       
    insert @tb select 'C','Z'update t
    set num=stuff(Num,5,1,(select top 1 col2 from @Tb where col1=substring(t.num,5,1)))
    from 
    @T tselect * from @t
    ID   NUM                  
    ---- -------------------- 
    213  213-HK25-JP91 (所影响的行数为 1 行)
      

  4.   

    ls,那个25还要加1 呢
    213  213-HK25-JP91213  213-HK26-JP91 
      

  5.   

    --加上+1declare @t table(ID char(3),NUM varchar(20))
    insert @t select '213','213-BK25-JP91 'declare @tb table(col1 char(1),col2 char(1))
    insert @tb select 'A','G'
    insert @tb select 'B','H'       
    insert @tb select 'C','Z'update t
    set num=stuff(Num,5,4,(select top 1 col2 from @Tb where col1=substring(t.num,5,1))+substring(t.num,6,1)+rtrim(substring(t.num,7,2)+1))
    from 
        @T tselect * from @t
    ID   NUM                  
    ---- -------------------- 
    213  213-HK26-JP91 (所影响的行数为 1 行)
      

  6.   

    刚回来,很高兴看到各位热心朋友的解答解释一下  NUM   格式例如   111-AK18-JP90     这样三段式的编号   但是 每个空格之间的字符数不是固定的  也可以是 111-AKG991-JPG119要求   将   表A里面   ID的第二位是   奇数的     NUM字段的第一个“-”符号后面的第一个字母按对照表B的方式对调   ,并将第一个“-”符号后面的最后一个数字+1 
    之能保证两个“-"之间的字符串是以字母开头,数字结尾。
    麻烦各位了,祝圣诞快乐!
      

  7.   

    我用的比较笨,奇数用个where子句 where substring(id,2,1)  in(1,3,5,7.9)限制一下,“每个空格之间的字符数不是固定的”这个地方把原来的上面他们写的具体数换成len函数求其长,然后调整就行了。