一个表TB内容如下:
mem_card_no
1009000001
1009000002
1009000003
1009000004
1009000005
1009000006
1009000007
1009000008
1009000009
1009000010
1009000011
1009000012
1009000013
1009000014
1009000015
1009000016
1009000017
1009000018
1009000019
1009000020
1009000021
1009000022
1009000023
1009000024
1009000025
1009000026
1009000027
1009000028
1009000029
1009000030
1009000031
1009000032
1009000033
1009000034
.
.
.
现在想把他更新为:
2011000001
2011000002
2011000003
2011000004
2011000005
2011000006
2011000007
2011000008
.
.
.求正解,简单要求就是将其中1009部分更新为2011,谢谢

解决方案 »

  1.   

    update TB set mem_card_no=replace(mem_card_no,'1009','2011') ...刚发完就找到答案了。。
      

  2.   

    update 表 set 字段=REPLACE(字段,'1009','2011')
      

  3.   


    update tb
    set col = '2011' + right(col,len(col)-4)
    where left(col,4) = '1009'
      

  4.   

    update tb set mem_card_no=stuff(mem_card_no,1,4,'2011')
      

  5.   

    replace容易出问题,建议还是用update tb set mem_card_no=stuff(mem_card_no,1,4,'2011')
      

  6.   


    declare @T table (mem_card_no varchar(16))
    insert into @T
    select 1009000001 union all
    select 1009000002 union all
    select 1009000003 union all
    select 1009000004 union all
    select 1009000005 union all
    select 1009000006 union all
    select 1009000007 union all
    select 1009000008 union all
    select 1009000009 union all
    select 1009000010 union all
    select 1010000011 union all
    select 1010000012 union all
    select 1010000013 union all
    select 1010000014 union all
    select 1010000015select * from @Tupdate @T set mem_card_no= case when left(mem_card_no,4)=1009
    then '2011'+right(mem_card_no,len(mem_card_no)-4) else mem_card_no endselect * from @T
    /*
    mem_card_no
    ----------------
    2011000001
    2011000002
    2011000003
    2011000004
    2011000005
    2011000006
    2011000007
    2011000008
    2011000009
    2011000010
    1010000011
    1010000012
    1010000013
    1010000014
    1010000015
    */
    --也可以用where 局部更新
    update tb set col = '2011' + right(col,len(col)-4)
    where left(col,4) = '1009'
      

  7.   

    update tb set mem_card_no=stuff(mem_card_no,1,4,'2011')
      

  8.   


    CREATE TABLE TB (
    mem_card_no VARCHAR(20)
    )INSERT INTO TB
    SELECT '1009000001'
    UNION ALL
    SELECT '1009000002'
    UNION ALL
    SELECT '1009000003'SELECT * FROM tbupdate tb set mem_card_no=replace(mem_card_no,'1009','2011')SELECT * FROM tbmem_card_no
    --------------------
    2011000001
    2011000002
    2011000003(3 row(s) affected)
      

  9.   


    update tb set mem_card_no=stuff(mem_card_no,1,4,'2011')
      

  10.   

    update TB set mem_card_no=replace(mem_card_no,'1009','2011') 
      

  11.   

    建议还是用小三的为好,repalce没限制条件的话很容易更新错误,使数据出错,这会很麻烦的
      

  12.   


    update TB 
    set mem_card_no='2011'+right(mem_card_no,len(mem_card_no)-4)
    where left(mem_card_no,4)='1009'楼主 你自己的的有点问题 慎用