all groups > sql server reporting services > january 2005 >
You're in the

sql server reporting services

group:

Iif statement to prevent divide by zero?


Iif statement to prevent divide by zero? plandry NO[at]SPAM newsgroups.nospam
1/3/2005 5:41:01 PM
sql server reporting services:
Hi- I'm trying to create a calculated field that is the percentage difference
between two database fields. To prevent a divide by zero, I tried making it:
= Iif( Fields!dsPrice.Value <> 0, (Fields!eePrice.Value -
Fields!dsPrice.Value) / Fields!dsPrice.Value, 1)
This should provide the % diff, or in the case that dsPrice is 0, 1 (100%).
When I try to run the report, however, it comes back as a divide by zero for
fields where dsPrice = 0. Does reporting services evaluate both portions of
the Iif, then output one? How do I avoid this divide by zero error?

Thanks in advance!
RE: Iif statement to prevent divide by zero? george
1/4/2005 8:25:04 AM
iif always evaluates both sides. try using the short circuit operator
'andalso' or 'orelse' in a function and add it to the code and call it from
Re: Iif statement to prevent divide by zero? Jeff A. Stucker
1/4/2005 10:28:16 AM
If the correct zero value is 100%, you can just move the pieces around like
this:

= Iif(Fields!dsPrice.Value = 0, 1, Fields!eePrice.Value ) /
Iif(Fields!eePrice.Value = 0, 1, Fields!dsPrice.Value)

That way, the division doesn't happen at all until the values are replaced.

--
Cheers,

'(' Jeff A. Stucker
\

Business Intelligence
www.criadvantage.com
--------------------------------------
"plandry@newsgroups.nospam"
[quoted text, click to view]

Re: Iif statement to prevent divide by zero? Jeff A. Stucker
1/4/2005 10:33:50 AM
Whoops, I think that should have been more like this:

= Iif(Fields!dsPrice.Value = 0, 1, Fields!eePrice.Value ) /
Iif(Fields!dsPrice.Value = 0, 1, Fields!dsPrice.Value)

Anyway, you get the idea!! :-)
--
Cheers,

'(' Jeff A. Stucker
\

Business Intelligence
www.criadvantage.com
--------------------------------------
[quoted text, click to view]

Re: Iif statement to prevent divide by zero? plandry NO[at]SPAM newsgroups.nospam
1/4/2005 3:35:02 PM
That did the trick... Thanks a bunch!

I will file that away in the "ninja reporting tricks" :)

[quoted text, click to view]
AddThis Social Bookmark Button