简体   繁体   中英

update column with condition from another column

I am trying to update a column based on the condition from another column. something like this, >

Attire app | SPV id | spv app

  | test | | test | | | | | 

expected:

Attire App | Spv id | SPV App

approved | test |

approved | test |

approved | | approved

approved | | approved

so, when column 2 is not null then update column 1, if column 2 is null then update column 1 and column 3.

here is my code :

        strcommand = "update tbl_Approve set [Attire App] = 'Approved', [Attire Date] = @ADate, [SPV app] = case when [spv id] is null then 'Approved' END where [attire app] is null"

on the visual I didn't add one other column named [attire date], assume that there is that column in the visual. but it doesn't work, it's only update column 1... do you have any suggestion about this problem?

thanks in advance.

I can't follow your code because it uses different column names from the description. However, the logic that you want is:

update yourtable
    set col1 = @COL1,
        col3 = (case when col2 = '' or col2 is null then @COL3 else col3 end);

Notice the else in the case statement. This sets the value of the column to itself -- so the value doesn't change.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM