devxlogo

Values Resulting to Null

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.

See also  Why ChatGPT Is So Important Today
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist