5

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?

2
  • 1
    Can 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? Commented 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.
    – goric
    Commented Feb 2, 2011 at 20:24

2 Answers 2

8

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.

4

Oracle provides NVL() for this scenario, ISNULL() is the equivalent in MS SQL Server. You could disguise all this in a VIEW to make your code clearer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.