ReportViewer Division by zero - reportviewer

I have some formulas in my reports, and to prevent divsion by zero I do like this in the expression field:
=IIF(Fields!F1.Value <> 0, Fields!F2.Value/Fields!F1.Value, 0)
This normally works fine, but when both F1 and F2 are zero, I get "#Error" in the report, and I get this warning: "The Value expression for the textbox ‘textbox196’ contains an error: Attempted to divide by zero."
Why is that?

IIF() is just a function, and like with any function all the arguments are evaluated before the function is called, including Fields!F2.Value/Fields!F1.Value. In other words, it will attempt to divide by zero in spite of the Fields!F1.Value <> 0 condition.

There has to be a prettier way than this, but this should work:
=IIF(Fields!F1.Value <> 0, Fields!F2.Value /
IIF(Fields!F1.Value <> 0, Fields!F1.Value, 42), 0)

However, you can use
if Fields!F1.Value <> 0
then
Fields!F2.Value/Fields!F1.Value
else 0
which should work, since it doesn't evaluate the then clause if the "if" section is false.

Related

Divide by zero error encountered, but should be excluded by CASE

I have the following code and I am getting an error:
Divide by zero error encountered.
SELECT
CASE WHEN SUM([monthly_qty]) = 0 THEN 999
ELSE ROUND(SUM([monthly_buy] * ([monthly_markup]+100)/100),2) * SUM([monthly_qty] / [monthly_qty]) END as [monthly_total]
FROM [xxxxx].[dbo].[quote_items] WHERE docid='10152'
The field that is causing the error is the second [monthly_qty], just before the END of the CASE statement.
SUM([monthly_qty] / **[monthly_qty]**)
The value of monthly_qty is zero, so the error makes sense, but I am confused as this field is inside the CASE statement, so the expected result is 999
Any help greatly appreciated.
I can't understand reason of some parts of your code like this:
SUM([monthly_qty] / [monthly_qty])
The result is error when [monthly_qty] = Zero, And 1 for non zero.
Anyway, You can set a default value When "monthly_qty" is zero:
IIF([monthly_qty]= 0,'YOUR_DEFAULT_VALUE', [monthly_qty] / [monthly_qty])
Then:
SUM(IIF([monthly_qty]= 0,'YOUR_DEFAULT_VALUE', [monthly_qty] / [monthly_qty]))
Have a read at MS Docs, there is an example of your case under Remarks.
The CASE expression evaluates its conditions sequentially and stops
with the first condition whose condition is satisfied. In some
situations, an expression is evaluated before a CASE expression
receives the results of the expression as its input. Errors in
evaluating these expressions are possible. Aggregate expressions that
appear in WHEN arguments to a CASE expression are evaluated first,
then provided to the CASE expression. For example, the following query
produces a divide by zero error when producing the value of the MAX
aggregate. This occurs prior to evaluating the CASE expression.
The reason is simple. This code:
SUM([monthly_qty]) = 0
Does not prevent a divide-by-zero in this code:
SUM([monthly_qty] / [monthly_qty])
The expressions are different.
The simplest solution (and essentially the "standard" approach) is to use NULLIF():
SUM([monthly_qty] / NULLIF([monthly_qty], 0))
However, I wonder if you really intend:
SUM([monthly_qty]) / NULLIF(SUM([monthly_qty]), 0)

isnull(#variable, 1) but for 0 instead of null

I have an equation that multiplies loads of variables together, if one of those variables is 0 then I don't want it included in the equation by substituting it for 1 which won't affect the result.
A case when - then, statement for each variable validating if they're greater than 0is a bit clunky.
Is there a similar function like IsNull where if the variable is 0 then it returns an alternate value?
--edit #Backs answer is right but apparently after sql 2012 iif was taken out, when i try to write the statement there is a syntax error at the '=' sign. Is there a replacement for iif after sql-2012?
IIF(#variable = 0, 1, #variable)

access statement convert to Sql

how can I convert to T-Sql this one?
IIf([ESSValue]<>0,Int([ESSValue]*100),"")
I think the following pretty much does what you want:
select coalesce(cast(EssValue * 100 as int), 0)
Here is the thinking. The comparison to zero is unimportant, because 0 times any value is going to be zero. The iif() returns an integer (I think) because the "then" argument is an integer; the empty string gets converted to zero.
I'm not 100% certain about the last statements with regard to MS Access, but that is how iif() works in SQL Server.
I should add. Although I don't approve of iif() for conditional expressions (because case is the standard and more powerful), SQL Server does support it. So you could write:
IIf([ESSValue]<>0, cast([ESSValue]*100 as int), '')
Note: As I mentioned earlier, the '' will be converted to 0.
CASE WHEN ESSValue <> 0
THEN CAST(ESSValue * 100 AS INT)
ELSE NULL
END as fieldname
For case expression the default is NULL if doesn't meet any condition, so you dont really need the ELSE condition

SQL DB2 null calculation causing problems

I have the following SQL:
Select dmvndn "Vendor Number", IFNULL(sum(dmsls) / sum(dmprc), 0) "Calculation"
From MyFile
Group By dmvndn
However, when i run this, i am still getting null records in my "Calculation" field.
I have also tried the COALESCE function, which returns the same results. I get some records as 0, and some records are blank (or, null).
Both fields are of type P, which i am told is packed numeric.
any ideas or suggestions?
Edit 1
It seems that the problem is not with either of these fields being NULL, it is that one or both fields are 0. And when i divide by zero, i get the empty / blank result.
Try
Sum(IFNULL(dmsls,0)) / Sum(IFNULL(dmprc,0))
A trick of this kind helps me in MS SQL Server:
Select
dmvndn "Vendor Number",
IFNULL(sum(dmsls) / NULLIF(sum(dmprc), 0), 0) "Calculation"
From MyFile
Group By dmvndn
I wonder if it can't help you in DB2.
UPDATE: an explanation.
Basically, it replaces the divisor with NULL if it's 0. And you may probably know that when at least one of the operands is NULL, the result of the operation becomes NULL as well.
To account for the result being NULL you already had your IFNULL on the result. It didn't make much difference then, because none of the operands was likely to be NULL. However, now using IFNULL makes perfect sense.

Why isn't my iif statement evaluating true when the condition is correct?

This is my statement:
iif(sum(Fields!myfield1.Value) = 0, 0, sum(Fields!myField2.Value)/sum(Fields!myField1.Value))
Any suggestions?
Likely it is evaluating as True. As mentioned in other comments, you'll get an error anyway because Iif() evaluates all parameter expressions regardless of the result of the test.
The error can be avoided by adding another Iif() in the divisor.
iif(
sum(Fields!myfield1.Value) = 0,
0,
sum(Fields!myField2.Value) / iif(
sum(Fields!myfield1.Value) = 0,
1,
sum(Fields!myField1.Value)
)
)
Now you'll get zero if myfield1 is zero and no error is thrown.
(You probably should show 'N/A' or just an empty string when the divisor is zero, though.)