SQL Server Central is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
Search:  
 
 

Handle Divide by Zero in SQL Reporting Services

By Clayton Groom, 2005/06/16

Total article views: 528 | Views in the last 30 days: 69

Out of the box, Reporting services does not handle divide by zero conditions gracefully.
There are plenty of posting on how to use the IIF() function to accomplish this, with some rather horrific looking code.
In order to simplify the coding process and make the resulting expressions readable by a mere mortal, I wrote the following function that can be included in any report.

Steps:

1. From the menu, choose Report, Report Properties.
2. Click on the Code tab and past the function code into the window.
3. Click on OK

To use the function, you have to reference the code collection in an expression:

=code.CalcRatio( Fields!PYGrossProfit.Value, Fields!PYSales.Value, Nothing)

Or if you want a zero instead of a blank:

=code.CalcRatio( Fields!PYGrossProfit.Value, Fields!PYSales.Value, 0)



By Clayton Groom, 2005/06/16

Total article views: 528 | Views in the last 30 days: 69
Your response
 
 
Related tags
 
Already registered?  

Free registration required

To read the rest of this article, and access thousands of other articles, we ask you to register on the site and subscribe to our newsletters.

Register

E-mail address:
Password:
Password (confirm):

  

Subscriptions

We ask you to register on the site and subscribe to our newsletters. Subscribing to our newsletters gets you:

  • ALL of our content (thousands of articles, scripts, and forum postings)
  • A daily newsletter (example)
  • A weekly news round up (example)
  • The opportunity to ask and answer questions in our forums
  • A daily Question of the Day to test and help you increase your knowledge of SQL Server.

We ask that you give the newsletter a try for a week. Over 200,000 SQL Server Professionals a day find it entertaining and useful. If not, you are welcome to unsubscribe at anytime.

Steve Jones
Editor, SQLServerCentral.com