Divide by zero error in SSRS Report - sql-server-2005

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.

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

Related

RDLC expression error when dividing two numbers

I have written an expression to get gross profit margin as follows.
=IIF(Sum(Fields!AugustValue.Value, "GrossProfitDataSet")-1>0 AND Sum(Fields!AugustValue.Value, "GrossSalesDataSet")-1>0,ROUND(((Sum(Fields!AugustValue.Value, "GrossProfitDataSet")-1)/(Sum(Fields!AugustValue.Value, "GrossSalesDataSet")-1))*100,2) & "%","0.00")
it shows #Error when the Sum(Fields!AugustValue.Value, "GrossProfitDataSet")-1 and the Sum(Fields!AugustValue.Value, "GrossProfitDataSet")-1 has zero. I have handled the zero values. Someone please help men to find the error
It is your Conversion related error. You need to convert AuguestValue to your desired any numeric data type if this not in numeric. Try with following expression
=IIF(Sum(Fields!AugustValue.Value, "GrossProfitDataSet")-1>0 AND Sum(Fields!AugustValue.Value, "GrossSalesDataSet")-1>0,ROUND(((Sum(Fields!AugustValue.Value, "GrossProfitDataSet")-1)/(Sum(Fields!AugustValue.Value, "GrossSalesDataSet")-1))*100,2) & "%","0.00")

Crystal Reports Division by zero but, having issue to just check zero to highlight zero

hi I'm trying to finish a crystal report, summary report, but I'm getting division by zero error. I've looked up everything and none of the options have helped me. The options already out there have only made my math formula turn everything to a zero.
if 1-({#dspStkWip}+{tblItem.OnOrderQuantity}/{#dspNeed})*100 > .15
then crYellow
else CrNocolor;
So whats happening since the need has zeros in the column it will end up making everything 0. I just need to find a way to implement a check for zero and to see if it needs to be highlighted in the column.
The {#dspNeed} field looks like the culprit to me. If this field contains 0 or NULL values then you would get the division by zero error you described.
Try this:
IF {#dspNeed} <> 0 THEN
if 1-({#dspStkWip}+{tblItem.OnOrderQuantity}/{#dspNeed})*100 > .15
then crYellow
else CrNocolor
ELSE
\\ code for how to handle values that throw division by zero error goes here

SSRS expression causing error

I have the following expression in SSRS:-
=IIf(IsNothing(Fields!Hours.Value), 0, Sum(Fields!Hours.Value) / Sum(Fields!ReportingHours.Value))
The expression appears as #Error. I removed the division part of the equation, leaving just this:
=IIf(IsNothing(Fields!Hours.Value), 0, Sum(Fields!Hours.Value))
Then the correct numbers appear. If I perform an addition operation, the calculation also errors out. It seems that it does not like something about performing mathematical operations when using the Sum() function. If I do simple division without the Sum, as in this expression:
=IIf(IsNothing(Fields!Hours.Value), 0, Fields!Hours.Value / Fields!ReportingHours.Value)
Then numbers show up fine. It is only when I have the Sum() function on the expression that causes an error. Any ideas as to how to fix this?
Try the following expression:-
=IIf(IsNothing(sum(Fields!Hours.Value)), 0, Sum(Fields!Hours.Value) / Sum(Fields!ReportingHours.Value)
EDIT
I have edited my previous post.

SQL CLR Aggregate function Error Handling

I have a user defined CLR aggregate function that can potentially throw an error. I would like to know how to handle an error if one occurs in my query.
The function is performing an IRR calculation similar to that which Excel does, ie. an iterative root-finding calculation. If no root is found, an error is thrown. This is the error I need to handle.
The query is part of a larger stored procedure and it looks something like:
select
MyID,
Excel_XIRR(col1)
from #t
group by MyID
and the error i get is something like this:
A .NET Framework error occurred during execution of user-defined routine or aggregate "Excel_Xirr":
System.ArgumentException: Not found an interval comprising the root after 60 tries, last tried was (-172638549748481000000000.000000, 280537643341281000000000.000000)
System.ArgumentException:
at System.Numeric.Common.rfindBounds#59(FastFunc`2 f, Double minBound, Double maxBound, Double precision, Double low, Double up, Int32 tries)
at System.Numeric.Common.findBounds(FastFunc`2 f, Double guess, Double minBound, Double maxBound, Double precision)
at System.Numeric.Common.findRoot(FastFunc`2 f, Double guess)
at Excel_Xirr.Terminate()
My problem is that not all the rows cause this error. There are some legitimate results from the query that I want to capture and use later in my stored procedure. Will this error stop me from getting the results of the query? If so, is there a way to figure out which rows throw the error (dynamically) and then rerun the query for the rest of the rows?
Not sure how well you have coded the XIRR function itself, looking at your function prototypes in Error messages it would seem you are using a Bi-section method of finding roots which is not most suitable to algorithms to use when it comes to finding rates. You will be locking yourself within a lower and upper bound no matter how large this bound is it is not going to help for all cases
As for solving your immediate problem with handling the error, you can change your .net code and replace the Throw...Exception statement with a return value of Math.Pow(-1, 0.5)
This will return a NAN to the calling program which you can then check with an IF statement to confirm whether your XIRR value is a number (when IRR is found) or a NAN value (when IRR is not found)

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.
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)
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.
I don't think your error is on the calculation. First of all, SSRS deals automatically with this situation. See my third column. And the forth shows your expression:
Your problem is probably somewhere else
This only seems to happens when the division is one of the results of an IIF, not if you just write a formula to divide one by the other, e.g.
=IIF(thing=1,10/0,0)
Before it has evaluated thing, it has already tried to calculate both results, causing an error. You can't use IIF in this way to protect from zero, you have to put the IIF on the bottom line of the division, e.g.
=IIF(thing=1, 10/IIF(divisor=0,-99999999999,divisor),0)
This is not satisfactory, since we've introcudes a weird small non zero number as the result, but it may be ok if you just want a non-error.
Technically, the #error is the correct answer.
Function IIF(arg1, arg2, arg3) always calculates all arguments, before returns a result, so your 2nd argument Fields!TotalPrice.Value/Fields!TotalSlots.Value can return #Error.
Try to use IF(arg1, arg2, arg3) function instead IIF.
=IF(Fields!TotalSlots.Value > 0,
Fields!TotalPrice.Value/Fields!TotalSlots.Value,
"unknown")