• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 196
  • Last Modified:

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
0
zell71
Asked:
zell71
  • 4
  • 2
1 Solution
 
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now