• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1406
  • Last Modified:

db2 sql sum a null value as zero.

I have a simple query,
a table A that joins on table B
I have to sum one filed of A with one filed of B,
If B doesnt match,
I dont want the result filed keep NULL value,
but only A field value.

In example:
if A field is 1 and B field is 1, the result field is 2.

if A field is 1 and B field is NULL (because it doesn match with A table key)
the result is NULL at the moment,
I would like it results 1.

there a function which transform null value in 0 in order to resolve the issue,
or are there other ways?

select A.valA, B.valB, A.valA+B.valB as Result from
A left join on A.key=B.key

Open in new window

1 Solution

select A.valA, B.valB, A.valA+ coalesce(B.valB,0) as Result from
A left join on A.key=B.key
bobdylan75Author Commented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now