Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding two column's data into one

Tags:

sql

sql-server

I have two columns in my report as below

ABC    XYZ
-------------
NULL   1
NULL   NULL
5      NULL 
4      8
NULL   1
8     NULL

I would like to add these two values and need final result as below

1
NULL
5
12
1
8

Please help.

Thanks in advance

like image 993
Bob Avatar asked May 22 '26 20:05

Bob


1 Answers

I think this is what you want.

Select 
      Case WHEN (ABC is null AND XYZ is null) THEN NULL 
           ELSE (Coalesce(ABC,0) + Coalesce(XYZ,0)) END as [sum]
from table
like image 51
Ajay Gupta Avatar answered May 24 '26 08:05

Ajay Gupta