Divide by zero error in SSRS Report

2019-07-30 16:24发布

问题:

I am writing a report in SQL Server 2005 Reporting Services, involving the division of money values that might be equal to zero. I put the following code in to check for a zero denominator:

=IIf(Sum(Fields!PreviousPremiumMTD.Value) = 0, "N/A", FormatPercent((Sum(Fields!PremiumMTD.Value) / Sum(Fields!PreviousPremiumMTD.Value))-1, 0))

However, for some reason I am still getting #Error displaying on my report with the following warning thrown:

[rsRuntimeErrorInExpression] The Value expression for the textbox ‘textbox62’ contains an error: Attempted to divide by zero.

Any assistance is greatly appreciated.

回答1:

IIF evaluates the expression before passing that to function, thats why you will always get the DivideByZero error here.

See an example post: http://secretgeek.net/iif_function.asp