SSRS expression - If null value then return 0 instead of blank
Asked Answered
B

6

11

I've seen a few examples but I just can't figure it out for my case. My expressions sums all values from field Total from the dataset AutoDeliveryCount. I need to reference the dataset since I'm using a few Total fields in my report. If the stored procedure returns null, how can I have my expression return 0 instead of a blank?

=Sum(Fields!Total.Value, "AutoDeliveryCount")
Benitez answered 30/1, 2015 at 18:36 Comment(0)
N
18
=IIf(IsNothing(Sum(Fields!Total.Value, "AutoDeliveryCount"))=True, 0, Sum(Fields!Total.Value, "AutoDeliveryCount"))
Nisus answered 30/1, 2015 at 20:48 Comment(2)
does this cause SSRS to evaluate the SUM() twice?Thoughtful
IsNothing returns Boolean. Thus, there is no need compare it with True. =IIf(IsNothing(Sum(Fields!Total.Value, "AutoDeliveryCount")), 0, Sum(Fields!Total.Value, "AutoDeliveryCount"))Ardie
M
21

Simply:

=Sum(Fields!Total.Value, "AutoDeliveryCount") + 0
Mediocre answered 30/8, 2019 at 17:19 Comment(2)
This the real MVP. Forget adding yet another annoying nested IIF wrapping around everything... just throw + 0 at the end. Brilliant.Jerkwater
Yep, less verbose and equally functional.Mediocre
N
18
=IIf(IsNothing(Sum(Fields!Total.Value, "AutoDeliveryCount"))=True, 0, Sum(Fields!Total.Value, "AutoDeliveryCount"))
Nisus answered 30/1, 2015 at 20:48 Comment(2)
does this cause SSRS to evaluate the SUM() twice?Thoughtful
IsNothing returns Boolean. Thus, there is no need compare it with True. =IIf(IsNothing(Sum(Fields!Total.Value, "AutoDeliveryCount")), 0, Sum(Fields!Total.Value, "AutoDeliveryCount"))Ardie
I
4

If you want to show 0 in integer column. Write Round(Fields!yourfield.Value ,2) to display as 0.00 . If your field is returning empty.

Inexact answered 9/11, 2017 at 9:8 Comment(0)
F
1

From the marked solution:
=IIf(IsNothing(Sum(Fields!Total.Value, "AutoDeliveryCount"))=True, 0, Sum(Fields!Total.Value, "AutoDeliveryCount"))

  1. IsNothing() always returns true or false and also IIF() always delivers true or false. So "=True" is redundant. IIf(IsNothing(Sum(Fields!Total.Value, "AutoDeliveryCount")), 0, ... works exactly the same way.

  2. What if Sum(Fields!Total.Value, "AutoDeliveryCount") returns 0? Since IsNothing() and 0 are being treated the same way 0 will be returned in both cases. It does not make any difference whether a field is empty or does contain the nubmer 0. How would you handle the difference between 0 and nothing?

Floccose answered 17/1, 2020 at 8:9 Comment(0)
D
0

Another option may be to use the Format property for the cell concerned. For example if you are displaying an Integer you could format it as:

#,##0

This will display 0 if the value is null/empty.

Dost answered 24/6, 2016 at 5:16 Comment(2)
This didn't work for me. Which SSRS version supports this?Benia
@Rashmi - SQL Server 2012. I developed the report using Visual Studio 2013 Update 5 with Microsoft SQL Server Data Tools - Business Intelligence for Visual Studio 2013 installed.Dost
B
0

I tried this formula in 2017 and it worked for me:

=IIF(Sum(Fields!Total.Value)=Nothing, 0, Sum(Fields!Total.Value, "AutoDeliveryCount"))
Bobolink answered 29/11, 2018 at 21:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.