# How to treat an empty dataset as zero for sum() in Expression

**URL:** <https://forum.inductiveautomation.com/t/how-to-treat-an-empty-dataset-as-zero-for-sum-in-expression/54366>\
**Category:** Ignition\
**Tags:** ignition79\
**Created:** [December 21, 2021, 7:30pm UTC](https://forum.inductiveautomation.com/t/how-to-treat-an-empty-dataset-as-zero-for-sum-in-expression/54366 "2021-12-21T19:30:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Troy](https://avatars.discourse-cdn.com/v4/letter/t/85f322/32.png) [@Troy](https://forum.inductiveautomation.com/u/Troy)\
**Post date:** [December 21, 2021, 7:30pm UTC](https://forum.inductiveautomation.com/t/how-to-treat-an-empty-dataset-as-zero-for-sum-in-expression/54366/1 "2021-12-21T19:30:37Z")

</div>

After upgrading from 7.9.3 to 7.9.18, I’ve been seeing some errors in our Expression code.  
“Error evaluating tag: Expression left operand is null.”

We have a UDT with several query tags, sometimes there are no rows in the dataset.  
In an expression tag we have:

```auto
sum({[.]SQLVolumeDS}, "converted_value")
+
sum({[.]SQLDilutionsVolDS}, "converted_value")
+
sum({[.]SQLTranserVolDS}, "converted_value")

```

I tried adding error handling but try(sum()) still returns null instead of zero, causing an error:

```auto
try(sum({[.]SQLVolumeDS}, "converted_value"), 0)
+
try(sum({[.]SQLDilutionsVolDS}, "converted_value"), 0)
+
try(sum({[.]SQLTranserVolDS}, "converted_value"), 0)

```

Any suggestions for getting this tag to calculate the total, even if any one of the datasets are empty?

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [December 21, 2021, 7:48pm UTC](https://forum.inductiveautomation.com/t/how-to-treat-an-empty-dataset-as-zero-for-sum-in-expression/54366/2 "2021-12-21T19:48:51Z")

</div>

> [@Troy](#):
>
> ```auto
> try(sum({[.]SQLVolumeDS}, "converted_value"), 0)
> +
> try(sum({[.]SQLDilutionsVolDS}, "converted_value"), 0)
> +
> try(sum({[.]SQLTranserVolDS}, "converted_value"), 0)
> 
> ```

Try this

```auto
try(sum({[.]SQLVolumeDS}, "converted_value"), 0) +
try(sum({[.]SQLDilutionsVolDS}, "converted_value"), 0) +
try(sum({[.]SQLTranserVolDS}, "converted_value"), 0)

```

I recall getting that error on upgrade too and I think this was the fix.

---

<div class="post-metadata">

**Author:** ![Troy](https://avatars.discourse-cdn.com/v4/letter/t/85f322/32.png) [@Troy](https://forum.inductiveautomation.com/u/Troy)\
**Post date:** [December 22, 2021, 3:58pm UTC](https://forum.inductiveautomation.com/t/how-to-treat-an-empty-dataset-as-zero-for-sum-in-expression/54366/3 "2021-12-22T15:58:10Z")

</div>

I still get the same error message

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [December 22, 2021, 4:02pm UTC](https://forum.inductiveautomation.com/t/how-to-treat-an-empty-dataset-as-zero-for-sum-in-expression/54366/4 "2021-12-22T16:02:24Z")

</div>

You could use `coalesce`, that returns the first non null value.  
If `sum({empty dataset})` returns null, then you could do  
`coalesce(sum({dataset1}), 0) + coalesce(sum({dataset2}), 0) + ...`

---

<div class="post-metadata">

**Author:** ![Troy](https://avatars.discourse-cdn.com/v4/letter/t/85f322/32.png) [@Troy](https://forum.inductiveautomation.com/u/Troy)\
**Post date:** [December 22, 2021, 4:29pm UTC](https://forum.inductiveautomation.com/t/how-to-treat-an-empty-dataset-as-zero-for-sum-in-expression/54366/5 "2021-12-22T16:29:02Z")

</div>

That worked, thanks!
