T-SQL selects a sum of two integer columns

I have two columns Val1 and Val2 as int in SQL Server 2008. When I select Tot = Val1 + Val2 from the table, I get null as Tot

How to get the total number of selected two columns?

Thank you very much.

+4
source share
2 answers

You probably have a null value in one of the columns. which will lead to a null result.

You can do the following if you want zero to represent 0.

SELECT Tot = ISNULL(Val1, 0) + ISNULL(Val2, 0) FROM Table 
+9
source

Use select Tot = isnull(Val1, 0) + isnull(Val2, 0) from Table

+2
source

All Articles