sql server - how can I update a list of records in a table with the new value depending on the old value -


i have table called acc1152 field accno. depending on value of field is, need replace new value. these values need update

old value   new value   7007        4007   7008        4008   4008        7   7009        4009   7011        4011   4011        ' '   7010        4010   4010        1   7016        4016   4016        1   4506        4006   4512        4012 

if record has old value above in accno field, accno needs replaced new value

how write 1 query accomplish this?

you can use case statements in update statement so:

update acc1152 set [columnname] =      case [columnname]         when '7007' '4007'         when '7008' '4008'         -- etc         when '4512' '4012'     else         [columnname] end 

or better way (imo) use temp/variable table keep track of old/new mappings

declare @sometable table (oldvalue varchar(50), newvalue varchar(50)) insert @sometable (oldvalue, newvalue) select '7007','4007' union select '7008','4008' -- etc union select '4512','4012'  update acc1152 set [columnname] = st.newvalue acc1152 inner join @sometable st on a.[columnname] = st.oldvalue 

Comments

Popular posts from this blog

javascript - Google App Script ContentService downloadAsFile not working -

javascript - Function overwritting -

php - Find a regex to take part of Email -