Problem solve Get help with specific problems with your technologies, process and projects.

Using ISNULL or NULLIF to prevent division by zero

How do I use the ISNULL or NULLIF to avoid division by zero?

ISNULL and NULLIF are different but related functions. ISNULL tests a value to see if it's null, and returns TRUE or FALSE accordingly. NULLIF compares two values and returns NULL if they're equal, otherwise it returns the first value. NULLIF(param1,param2) is equivalent to CASE WHEN param1 = param2 THEN NULL ELSE param1 END

Important Note: NULLIF is standard SQL-92; ISNULL isn't, but you can use the phrase IS NOT NULL in a WHERE clause instead.

Different database systems offer a range of non-standard functions that are often useful. In MySQL, you can use ISNULL, NULLIF, and even IFNULL. In Microsoft SQL/Server you can use ISNULL and NULLIF, but in Oracle you have to use NVL and DECODE respectively.

In any case, all you need for this problem is ISNULL. (NULLIF will have to wait for another question.)

When you do division in SQL, there are two things to watch out for -- nulls and zero. You should anticipate both situations in your query. If the column was defined as NOT NULL, you don't have to worry about nulls, but if the column is "nullable" (i.e. defined as NULL, or not NOT NULL), you must be careful about dividing by a null value because the answer will be null. Sometimes that's okay, but often you'd like to exclude those rows from the query. Also, you sure wouldn't want to divide by zero, since that will cause either a data exception or a null value. So the best strategy is to check both conditions --

select Hits / AtBats as HittingPct
  from yourTable
 where AtBats IS NOT NULL
   and AtBats > 0

If the column was defined NOT NULL, you can drop the first of the above WHERE clause conditions.

("But what if somebody hasn't had any at bats?" I can hear you asking. Well, they won't be in that result set. If they need to be, then you have to ask yourself what their hitting percentage should be. It can't be .000, can it? Would that be fair? One might say it was NULL, unknown, not applicable, indeterminate, or yet-to-be-decided. You can achieve this result, getting nulls wherever the divisor is zero, by removing the WHERE clause, but only if your database returns a null for an attempted division by zero (the proper behaviour, in my estimation), instead of hiccoughing an error message that informs you that you just tried to divide by zero. Then you wouldn't need to filter out the people who hadn't had any at bats yet. After all, filtering out rows based on zero conditions can get tiresome, especially since you have to do it in every query where you have a division calculation, such as hitting percentage, which you obviously want to calculate on the fly. You could build a view which has the filter condition built in -- this eliminates the problem of dividing by zero in SQL, but creates the situation where some people are missing from certain queries.)

For More Information

This was last published in February 2002

Dig Deeper on Oracle and SQL

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

Start the conversation

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.