Solved

# SQL Case statement problem

Posted on 2011-09-28
242 Views
I have this :

UPDATE Nop_ProductVariant
SET Price = OldPrice + 2.00 + (
CASE
WHEN OldPrice              < 5 THEN 5.00 - (OldPrice - ProductCost)
WHEN OldPrice BETWEEN 5 AND 10 THEN 8.00 - (OldPrice - ProductCost)
WHEN OldPrice BETWEEN 10 AND 50 THEN 12.00 - (OldPrice - ProductCost)
END

But need to add another condition :

CASE
WHEN OldPrice              < 5 THEN 5.00 - (OldPrice - ProductCost)

If (OldPrice - ProductCost) < 5.00
THEN 5.00 - (OldPrice - ProductCost)
ELSE OldPrice

WHEN OldPrice BETWEEN 5 AND 10 THEN 8.00 - (OldPrice - ProductCost)

If (OldPrice - ProductCost) < 8.00
THEN 8.00 - (OldPrice - ProductCost)
ELSE OldPrice

WHEN OldPrice BETWEEN 10 AND 50 THEN 12.00 - (OldPrice - ProductCost)

If (OldPrice - ProductCost) < 12.00
THEN 12.00 - (OldPrice - ProductCost)
ELSE OldPrice

END

Can I still use the Case statement to do this? thanks

0
Question by:MikeMCSD
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2

LVL 14

Expert Comment

ID: 36718590
You can nest Case Statements.  Not 100% sure if I followed your logic, but here is an example.
``````declare @myCosts table (Id int identity(1,1), myPrice money, myOldPrice money, myCalculatedPrice money)

insert into @myCosts values (6.00, 5.00, null)
insert into @myCosts values (10.00, 12.00, null)
insert into @myCosts values (45.00, 40.00, null)
insert into @myCosts values (2.00, 3.00, null)
insert into @myCosts values (24.00, 21.00, null)

update @myCosts

set myCalculatedPrice =

case
--if Old Price < 5
when myOldPrice < 5 then

case
--the variance of "my old price" and "price" < 5 so subtract the variance from 5
when (myOldPrice - myPrice) < 5 then (5 - (myOldPrice - myPrice))

--the variance of "my old price" and "price" >- 5 so return myOldPrice
else myOldPrice
end

when myOldPrice < 10 then

case
--the variance of "my old price" and "price" < 5 so subtract the variance from 5
when (myOldPrice - myPrice) < 8 then (8 - (myOldPrice - myPrice))

--the variance of "my old price" and "price" >- 5 so return myOldPrice
else myOldPrice
end

when myOldPrice < 50 then

case
--the variance of "my old price" and "price" < 5 so subtract the variance from 5
when (myOldPrice - myPrice) < 12 then (12 - (myOldPrice - myPrice))

--the variance of "my old price" and "price" >- 5 so return myOldPrice
else myOldPrice
end

end

from @myCosts

select *
from	@myCosts
``````
0

LVL 16

Author Comment

ID: 36718745
thanks gohord, ..  good idea about nesting.
I think this will work, I'll test it :

SET Price = OldPrice + 2.00 + (
CASE

WHEN OldPrice              < 5 THEN
CASE
WHEN (OldPrice - ProductCost) < 5.00  THEN 5.00 - (OldPrice - ProductCost)
ELSE OldPrice
END

WHEN OldPrice BETWEEN 5 AND 10 THEN
CASE
WHEN (OldPrice - ProductCost) < 8.00  THEN 8.00 - (OldPrice - ProductCost)
ELSE OldPrice
END

WHEN OldPrice BETWEEN 10 AND 50 THEN
CASE
WHEN (OldPrice - ProductCost) < 12.00  THEN 12.00 - (OldPrice - ProductCost)
ELSE OldPrice
END

END
0

LVL 14

Accepted Solution

Christopher Gordon earned 500 total points
ID: 36718808
Just a heads up, a case statement will automatically exit when it reaches a condition that is TRUE.

Because of this, the BETWEEN syntax isn't necessary.   You'll notice in my sample that I didn't use BETWEEN.

I guess it just boils down to what is easier for you (or the next programmer) to understand.  I honestly couldn't tell you if there was a best practice on which way to go.

0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have a large data set and a SSIS package. How can I load this file in multi threading?
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
###### Suggested Courses
Course of the Month1 day, 17 hours left to enroll