Values Resulting to Null

Question:
I want to add two numeric fields with values like these:

Example 1: One field named Amount contains 565.25 and other field named Ticket contains null.

 select amount + ticket 

The result fetched is null. But the result should be 565.25 instead of null.

Example 2:

 Select 565.25 + null

Results to null as well. How can I fix this ?

Answer:
As you have discovered, the result of any operation where an operand is null is NULL!

This is overcome by taking a little extra care in your table create statement. What is required is provision for a default value against a column that is not allowed to be null. In your case, you need a default value of 0 (zero) so that you can derive the sums you envision. This will not work if one of the parties to the operation is null.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: