Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL: sum 3 columns when one column has a null value?

Tags:

sql

SELECT      sum(TotalHoursM)           + (TotalHoursT)           + (TotalHoursW)           + (TotalHoursTH)           + (TotalHoursF)            AS TOTAL FROM LeaveRequest 
like image 616
Yves Avatar asked Jul 06 '09 18:07

Yves


People also ask

How do I sum a column with NULL values in SQL?

In MySQL, SUM() is supposed to ignore null values. In any case, if you want to be specific, you can instruct the SQL Optimiser to substitute the null values with the value of 0. To do that, you can help yourself with the COALESCE() function and the following model: SELECT COALESCE(SUM(column_name), 0) ...

Does Sum work with NULL?

Yes its safe . You can use Sum without handling NULL Value.

Does SQL sum Ignore NULL?

SUM can be used with numeric columns only. Null values are ignored.


1 Answers

If the column has a 0 value, you are fine, my guess is that you have a problem with a Null value, in that case you would need to use IsNull(Column, 0) to ensure it is always 0 at minimum.

like image 199
Mitchel Sellers Avatar answered Oct 13 '22 22:10

Mitchel Sellers