1.如 表 F 字段B 数据是王1,王2,王3。。王N,怎么用UPDATE更新成A1,A2,A3..........AN的2.表F 字段B 数据是王,王,王,王。。王 怎么用UPDATE更新成W1,W2,W3.......WN的本人是菜鸟,求大神帮帮忙。

解决方案 »

  1.   

    update f
    set b = replace(b,'王','A')
      

  2.   


    select replace(字段B,'王','A') into #temp from 表F  update 表F a  set a.字段B=b.字段B  from 表F a ,#temp  b  
      

  3.   


    declare @t table(id int identity(1,1),[c1] varchar(4),[c2] varchar(6))
    insert @t
    select '王1','王' union all
    select '王2','王' union all
    select '王3','王' union all
    select '王N','王'update @t set [c1]=replace([c1],'王','A')
    update @t set [c2]='w'+ltrim(id)select * from @t
    /*
    id          c1   c2
    ----------- ---- ------
    1           A1   w1
    2           A2   w2
    3           A3   w3
    4           AN   w4
    */--id不连续的话用row_number() 生成连续id号即可。
      

  4.   


    --不好意思,上面有误
    IF  exists (select 1  from  sysobjects where name='[F]'and type='p')  
    drop table  [F]
    go
    create table F (B varchar(4))
    insert  [F]
    select '王1'union all
    select '王2'union all
    select '王3'union all
    select '王4'union all
    select '王5'--  select  * from  F
    --1
    update F set B=replace(B,'A','王')  
    --2
    select B=replace(B,'王','A') into #temp from F  update F set B=b.B  from F,#temp  b/*
       B
       王1
       王2
       王3
       王4
       王5
    (所影响的行数为 5 行)
    */
      

  5.   

    declare @t table(id int identity(1,1),[c1] varchar(4),[c2] varchar(6))
    insert @t
    select '王1','王' union all
    select '王2','王' union all
    select '王3','王' union all
    select '王N','王'
    UPDATE @t
    SET [c1] = REPLACE([c1],'王','A'),
    [c2] = REPLACE([c2],'王','W'+LTRIM(id))
      

  6.   

    -----------------------------------------------------------------
    /**
     * 测试 3 楼 报错,也许是我理解错了,望指点
     * 测试row_number()函数
     * 2012-8-14
     * 
    **/
    -----------------------------------------------------------------
    declare @t table(id int identity(1,1),[c1] varchar(4),[c2] varchar(6))
    insert @t
    select '王1','王' union all
    select '王2','王' union all
    select '王3','王' union all
    select '王N','王'update @t set [c1]=replace([c1],'王','A')
    update @t set [c2]='w'+ltrim(ROW_NUMBER()OVER(ORDER BY id))SELECT * FROM @t------------------------------------------------------报错:
         消息 4108,级别 15,状态 1,第 9 行
    开窗函数只能出现在 SELECT 或 ORDER BY 子句中。
      

  7.   

    -----------------------------------------------------------------
    /**
     * 测试 3楼 ,也许我理解错了,望指点
     * 测试row_number()函数
     * 2012-8-14
     * 
    **/
    -----------------------------------------------------------------
    declare @t table(id int identity(1,1),[c1] varchar(4),[c2] varchar(6))
    insert @t
    select '王1','王' union all
    select '王2','王' union all
    select '王3','王' union all
    select '王N','王'update @t set [c2]='w'+ltrim(ROW_NUMBER()OVER(ORDER BY id))SELECT * FROM @t报错:
    消息 4108,级别 15,状态 1,第 9 行
    开窗函数只能出现在 SELECT 或 ORDER BY 子句中。
      

  8.   

    -----------------------------------------------------------------
    /**
     * 测试 3 楼 ,也许理解错了,没出来,望指点
     * 测试row_number()函数
     * 2012-8-14
     * 
    **/
    -----------------------------------------------------------------
    declare @t table(id int identity(1,1),[c1] varchar(4),[c2] varchar(6))
    insert @t
    select '王1','王' union all
    select '王2','王' union all
    select '王3','王' union all
    select '王N','王'
    update @t set [c2]='w'+ltrim(ROW_NUMBER()OVER(ORDER BY id))SELECT * FROM @t报错:消息 4108,级别 15,状态 1,第 9 行
    开窗函数只能出现在 SELECT 或 ORDER BY 子句中。
      

  9.   


    --假设ID不连续
    declare @t table(id int,[c1] varchar(4),[c2] varchar(6))
    insert @t
    select 2,'王1','王' union all
    select 4,'王2','王' union all
    select 7,'王3','王' union all
    select 10,'王N','王'update @t set [c1]=replace([c1],'王','A')
    update @t set [c2]='W'+ltrim(b.rid)
    from @t a left join (select id,row_number() over (order by id) as rid from @t) b
    on a.id=b.idselect * from @t
    /*
    id          c1   c2
    ----------- ---- ------
    2           A1   W1
    4           A2   W2
    7           A3   W3
    10          AN   W4
    */
      

  10.   

    replace 才是王道,row_number()是2005里面才有的
      

  11.   

    还是 replace 好用 但是如果我想指定位置的数据被替换应该怎么弄呢
    比如 表 table
    name  phone
    1     313464614
    2     35464645
    3     2464674
    4     25465464
    5     4514964把phone的第一位全部替换成9
    求SQL
      

  12.   

    update table set phone=replace(phone,substr(phone,1,1),'9')  自己高出来了 呵呵
      

  13.   

    update F SET B=replace(B,'王','A')
      

  14.   

    都是用replace吗?那补充请教个问题:比如 想更新A字段的1-4位 是一个变量值,5-8位是另外一个变量值,如何UPDATE?