Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Conditional UPDATE Based on SELECT

In SQL Server 2008, I have a set of data containing costs for East and West. I'm adding a new field for a customer for Canada which needs to be 1.5 times the East or West cost (which ever is greater). So I'm trying to come up with some sql I can execute. I've tried the following but have not had success:

 UPDATE ShippingCost

 SET

    IF EastCost>WestCost

       Canada= EastCost*1.8

    ELSE

       Canada= WestCost*1.8
    ENDIF

I'm sure there's an easy way to do this? Any ideas?

like image 469
Damon Avatar asked Jan 25 '12 16:01

Damon


1 Answers

You need to use Case

 UPDATE ShippingCost

 SET
     Canada = CASE WHEN EastCost>WestCost THEN  EastCost*1.8
                   ELSE WestCost*1.8 END
like image 81
Conrad Frix Avatar answered Sep 20 '22 11:09

Conrad Frix