Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

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?

share|improve this question
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? –  Eric Humphrey - lotsahelp Feb 2 '11 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 Feb 2 '11 at 20:24
add comment

2 Answers

up vote 6 down vote accepted

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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