Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TSQL Conditional Set

I want to set a field in my table based on another field in the table.

This is the functionality that I want:

set result = Win if ((select status from tableY) like '%Won%')
set result = Loss if ((select status from tableY) like '%lost%')

This does not compile... How do I get the correct functionality?

like image 747
CodeKingPlusPlus Avatar asked Mar 11 '26 01:03

CodeKingPlusPlus


2 Answers

set result = case when (select status from tableY) like '%Won%'
                  then 'Win'
                  when (select status from tableY) like '%lost%'
                  then 'Lost'
               -- If neither win or lose don't change a thing
                  else result
              end
like image 198
Nikola Markovinović Avatar answered Mar 14 '26 06:03

Nikola Markovinović


So, you're trying to update a column in tableY?

UPDATE tableY SET Result = CASE WHEN status LIKE '%Won%' then 'Win' 
                                ELSE 'Loss'
                           END
WHERE (status LIKE '%Won%'  AND COALESCE(Result,'') != 'Win')
   OR (status LIKE '%Lost%' AND COALESCE(Result,'') != 'Loss')
like image 43
GilM Avatar answered Mar 14 '26 06:03

GilM



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!