Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

divide by zero/null workaround in SSRS 2008 report

I have a report with a field whose value was the expression:

Fields!TotalPrice.Value/Fields!TotalSlots.Value

Although sometimes TotalSlots was blank and thus I was getting a divide by zero runtime error. So I changed the expression to this:

=IIF(Fields!TotalSlots.Value > 0, Fields!TotalPrice.Value/Fields!TotalSlots.Value,"unknown")

but I'm still getting a divide by zero error. How do I work around this zero divisor issue.

like image 803
jsmith Avatar asked May 03 '12 13:05

jsmith


People also ask

How do you display zeros as blanks in a SSRS report?

You can use FORMAT function to format numbers, e.g. =Format(Fields! MyField. Value,"0.00").

Is nothing in SSRS expression?

The SSRS expression language is basically VB. According to the MSDN page on Nothing: When checking whether a reference (or nullable value type) variable is null, do not use = Nothing or <> Nothing. Always use Is Nothing or IsNot Nothing.

IS NULL check in SSRS?

How to check if a parameter is NULL in SSRS? The IsNothing() function returns True if the Paramter value is NULL , otherwise it will return FALSE.


2 Answers

The VB IIF evaluates all arguments, so it will throw an error if any argument throws an error:

Your formula can be written as:

=IIF(Fields!TotalSlots.Value > 0,
   Fields!TotalPrice.Value /
   IIF(Fields!TotalSlots.Value > 0,
       Fields!TotalSlots.Value,
       1 ),
   "unknown")

Then even when TotalSlots is zero, the formula still won't encounter a division problem.

like image 61
Jamie F Avatar answered Sep 18 '22 13:09

Jamie F


Jamie F's answer is correct. As a tip, you can add a function to your report code to make the division a bit easier to implement in multiple cells, e.g.

Public Function Divider (ByVal Dividend As Double, ByVal Divisor As Double)
If IsNothing(Divisor) Or Divisor = 0
  Return 0
Else
  Return Dividend/Divisor
End If
End Function 

You can then call this in a cell like so:

=Code.Divider(Fields!FieldA.Value, Fields!FieldB.Value)
like image 33
Nathan Griffiths Avatar answered Sep 19 '22 13:09

Nathan Griffiths