Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple condition update statement in SQL Server

I know how to update where it is an either/or situation. However, if I need to add more conditions to a statement I am not sure how to proceed.

The desired task is updating 1 to 10, 2 to 20, 3 to 30, 5 to 50, and 0 to 00. Would it be something like such:

update table 
set own = (case 
              when own in '1' then '10, 
           case 
              when own in'2' then '20'.....else '00' 
           end);

Or would it be best to break it down to five separate update statements where 1, 2,3,5,and 0 are dealt with separately?

Desired Result

own     own
 1      10
 2      20
 3      30
 5      50
 0      00
like image 949
Tim Wilcox Avatar asked Mar 08 '26 04:03

Tim Wilcox


2 Answers

You only need a single case statement. I used = versus in since you aren't supplying multiple values.

update table 
set own=case 
            when own = '1' then '10'
            when own = '2' then '20'
            when own = '3' then '30'
            ....
            else '00' 
        End
like image 163
S3S Avatar answered Mar 09 '26 18:03

S3S


If your problem is only to add an '0'

try it:

update table set own = RTRIM(LTRIM(own)) + '0';
like image 23
Maurício Pontalti Neri Avatar answered Mar 09 '26 17:03

Maurício Pontalti Neri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!