sql - Insert NULL if value matches the value of another column -
lets have table : "mytable" , have 2 columns in : "val" , "val_new".
now want insert new value "val_new" if values equal('val' , 'val_new') want insert null instead.
---------------------- | id | val | val_new | ---------------------- | 1 | 5 | null | ---------------------- | 2 | 6 | null | ----------------------
lets have table example.
now :
update mytable mt set mt.val_new = '5' mt.id = '1';
i want value of val_new remain null or updated null instead of '5'.
edit: want update existing values not inserting new rows.
you need not have insert record rather have update existing one. if run insert command new record created. table have
---------------- |val | val_new | ---------------- | 5 | null | ---------------- | 6 | null | ---------------- | | | <-- if val = val_new ---------------- | | 6 | <==if val<> val_new.
i guess dont need output. best option update columns.
you can use case statement,
update <yourtable> set val_new =case when val_new= val null else val_new end
Comments
Post a Comment