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
How to Group by Month from Date Field Using SQL
Why Is My T-SQL Left Join Not Working
Use Select Inside an Update Query
Sqlplus Statement from Command Line
Discard Millisecond Part from Timestamp
How to Add 'On Delete Cascade' in Alter Table Statement
SQL Server 2008- Get Table Constraints
Cannot Create an Instance of Ole Db Provider Microsoft.Jet.Oledb.4.0 for Linked Server Null
Split String by Delimiter Position Using Oracle SQL
What Are the Principles Behind, and Benefits Of, the "Party Model"
How Exactly Does Using or in a MySQL Statement Differ With/Without Parentheses
Custom Function with Check Constraint SQL Server 2008
Mysql: "The Select Would Examine More Than Max_Join_Size Rows"