SUM(Field) = NULL - how to get it to = 0
Posted on 2006-11-20
I've got something that's probably simple, but I'm still not that great at SQL...
SELECT SUM(field1) + SUM(field2) AS 'Total' FROM Table WHERE field3 = 'something'
Problem is, sometimes, field1 or field2 may not have any entries when field3 = 'something'.
field1 field2 field3
10 20 b
NULL 15 a
5 30 b
NULL 25 a
Now the problem is, using the above query, SELECT SUM(field1) + SUM(field2) AS 'Total' FROM Table WHERE field3 = 'a' Results in NULL and not what I need, 40. I was thinking something like CASE WHEN Sum(field1) IS NULL THEN 0 - but I think I have the syntax wrong...