SQL if statement?

hi, using a sql script to calc a margin as a %..
so its something like:

((TotalMargin + TotalRefundedMargin) / nullif((TotalNetSales+ TotalRefundedSales),0)) * 100 AS [Margin%],

however, the problem occurs when TotalNetSales+ TotalRefundedSales < 0 and TotalMargin + TotalRefundedMargin < 0..
how can i do a check to see if both these figures are less than 0...set Margin% as 0 (zero)?

thanks
LVL 6
zell71Asked:
Who is Participating?
 
silemoneCommented:
use case statement...Case   when   <-- used like if statements...
0
 
silemoneCommented:
here's a link to show you how to use:

http://technet.microsoft.com/en-us/library/ms181765.aspx
0
 
Patrick MatthewsCommented:
In SQL Server, you would use CASE...


CASE WHEN (TotalNetSales + TotalRefundedSales) < 0 AND (TotalMargin + TotalRefundedMargin) < 0 THEN
     ELSE
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Patrick MatthewsCommented:
Too slow :)
0
 
silemoneCommented:
here if/else...this example should be exactly what you're looking for...has a similar expression in it...

http://doc.ddart.net/mssql/sql70/ia-iz_4.htm
0
 
silemoneCommented:
but again, i as stated, most times, Case when statement but if you want if/else, there you have it in the above link...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.