Currently whenever I write a query that is adding columns that can contain nulls I resort to wrapping each field in isnull
or coalesce
, such as coalesce(score1,0) + coalesce(score2,0)
.
Is there a better way to handle this, or is this the standard practice?
-
1Can you elaborate? Are these insert / update statements? Are you adding a nullable column to a table, and want a default for existing records or new records?– Eric Humphrey - lotsahelpCommented Feb 2, 2011 at 20:13
-
@Eric Humphrey: Edited the question for clarity. The question was meant for querying columns that can contain nulls, and wanting to add some of them together.– goricCommented Feb 2, 2011 at 20:24
Add a comment
|
2 Answers
That's pretty much what you have to do. Since NULL + anything else is NULL, you have to wrap each column in its own isnull(), coalesce() or CASE.