Divide by Zero/Null Workaround in Ssrs 2008 Report

divide by zero/null workaround in SSRS 2008 report

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.

SSRS 2008- Dealing with division by NULL Values

Use an IIF (inline If statement) in the expression:

=IIF(<Condition>,<TruePart>,<FalsePart>)

=IIF(Field_A Is Nothing OR Field_B = 0, 0, Field_A/Field_B)

This way, you are making sure that Field_A is not null and Field_B is not zero, to also avoid divide by zero errors.

SSRS 2008 - Dealing with division by zero scenarios

IIf will always evaluate both results before deciding which one to actually return.

Try

=IIf(Fields!SomeField.Value = 0, 0, Fields!SomeOtherField.Value / IIf(Fields!SomeField.Value = 0, 1, Fields!SomeField.Value))

This will use 1 as the divisor if SomeOtherField.Value = 0, which does not generate an error. The parent IIf will return the correct 0 for the overall expression.

Avoid divide by zero in SSRS expression

This should work for null and 0.

=IIF(Fields!TotalSales.Value <> 0 , (Fields!Margin.Value) / (Fields!TotalSales.Value) * 100 , 0)

SSRS Expression Divide by Zero Error

You can add a function to your report code that handles the divide by zero condition, this makes it 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)

SSRS Divided by zero error

Alternatively, you could use the following custom code in your report and then call it in your report (right click report, report Properties - > Code

Public Shared Function VarPercent(ByVal Standard As Decimal, ByVal Actual As Decimal) As Decimal
If Actual = 0 Then
Return 0
End If
If Standard = 0 Then
Return 0
End If
Return (Standard / Actual )
End Function

Then you can call it like this in your report (where ever you are dividing)

Code.VarPercent (Fields!numerator.Value, Fields!denominator.Value)


Related Topics



Leave a reply



Submit