Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: SQL Server
Expertise: Beginner
Nov 30, 1999



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Values Resulting to Null

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 ?

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.

DevX Pro
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date