Skip to content
Advertisement

Showing null instead of throwing a zero division error by try-catch

Why does this procedure return NULL when count and sum are both 0?

and does not produce output like this?

How to change the first procedure to act same as the second one when there is nothing in the table, meaning when sum and count are both 0?

Thanks.

Advertisement

Answer

If there is nothing in the table then you have no rows. In that case, the SUM() returns NULL, not 0.

If you want an error to occur, then replace it with a value:

The count() will still return 0, so this should generate an error.

Note that avg() would be a more sensible way to do the calculation.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement