Merka Merka - 2 months ago 7
SQL Question

Update column value with diiferent condition

1) I want to update value from column "Order_name" from table 'Orders" after checking some condition (Area, Condition_flag)

Example:

when Area='1' and Condition_flag=C then Order_name= 'Num_Order'+'_'+'Condition_flag'


2) Update Store_code
when Area ='2' and Condition_flag='T'

Answer

It may fulfill your both requirements

UPDATE t SET 
    Order_name = CASE WHEN t.Area='1' and t.Condition_flag = 'C' THEN CONCAT(Num_Order, '-', Condition_flag) ELSE  Order_name END,
    Store_code = CASE WHEN t.Area='2' and t.Condition_flag = 'T' THEN 'Value Goes Here' ELSE  Store_code END
FROM #TableName t
WHERE t.area IN ('1','2')
    AND t.condition_flag IN ('c', 't')

but please adjust values if integer then quote not required otherwise is must be.

Comments