IFNULL is not working

Asked

Viewed 59 times

0

    select IFNULL(valor_unit,0) from tb_itens where id = 3  

In the scenario where the id = 3 does not exist in my table the SELECT does not return '0' just keeps returning NULL, I can’t find the problem.
I’ve tested with COALESCE, but keeps returning null.

1 answer

5

In the scenario where id = 3 does not exist in my table SELECT does not returns '0' just keeps returning NULL

Of course, the function will check the value of the field, but if you have already limited in the where, not bringing what may be null because it does not exist, will not come the result.

The function isnull should be using when a value may be null, if removing the where must bring the right result.

  • Uhm, I got it. I thought that by not finding some value, juxtaposition by the Where condition imposed, it would return me null (in case '0'). Thank you.

  • Yes, it makes sense your reasoning, but as the where is processed before the select, that only returns what was filtered, will never get to process a null :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.