kodder360
kodder360

Reputation: 99

How to display sum result of two column field even if one field is empty in sql?

I am having difficulty to show total value of two fileds in sql. I am getting result of first row but my second row is not showing any result just displaying empty.

Tabel Name: NumberTable

ID  VALUE1  VALUE2
===================
1   1       2
2   2 

Sql Query

Select id, (VALUE1  + VALUE2) as Total_Salary  From NumberTable; 

The following output that I am getting after running this query

ID  TOTAL
1    3 
2    -

But I want output like this:

ID  TOTAL
1    3 
2    2 

Upvotes: 0

Views: 309

Answers (2)

Jim Macaulay
Jim Macaulay

Reputation: 5141

Please use NVL function,

Select id, (NVL(VALUE1, 0)  + NVL(VALUE2, 0)) as Total_Salary  From NumberTable; 

Upvotes: 0

Gordon Linoff
Gordon Linoff

Reputation: 1269773

Use coalesce():

coalesce(value1, 0) + coalesce(value2, 0)

The addition operator returns NULL if either argument is NULL.

Upvotes: 1

Related Questions