[转]SSRS: Checking for Divide By Zero Using Custom Code

本文介绍了在SSRS报告中遇到零除错误的问题,并详细解释了如何使用自定义代码来解决这一问题。通过创建自定义函数Divide,可以有效地避免在报告计算过程中出现零除错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文转自:http://salvoz.com/blog/2011/11/25/ssrs-checking-for-divide-by-zero-using-custom-code/

I encountered a divide-by-zero error while working on an SSRS report and thought the issue could easily be resolved using IIF with code similar to the following:

=IIF(Fields!Denominator.Value = 0, 0, Fields!Numerator.Value/Fields!Denominator.Value)

I soon realized that this does not resolve the issue.  It appears that all parameters  in the IIF function are evaluated regardless if the first parameter evaluates to true or false.  Therefore, the divide-by-zero was still occurring.

After doing some research, I decided that the best option to avoid the divide-by-zero error is to implement custom code.

Note: The following screen shots are from Report Builder 3.0

The first step is to open the Report Properties window.  You can access the report properties by clicking anywhere outside of the report body.

If you still cannot see the Report Properties window, make sure you have the ‘Properties’ option checked in the ‘View’ tab.

image

The Report Properties window is displayed below.  In the Code text box, click the ellipse […].  You may need to click on the Code text box first to see the ellipse button.

image

Next, select ‘Code’ in the left hand menu if it is not already selected.  Paste the code (displayed below screen shot) in the Custom code field.

SNAGHTML9a4f0b7

Function Divide(Numerator as Double, Denominator as Double) If Denominator = 0 Then Return 0 Else Return Numerator/Denominator End If End Function

Now that you’ve created the custom code, you can begin to use the code in your report.  The following is an example of how you can use the Divide function in a text box expression:

=Code.Divide(Fields!CurrentYearSales.Value-Fields!PriorYearSales.Value,Fields!PriorYearSales.Value)*100

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值