44 Votes

MySQL: How to use IFNULL - Incorrect Parameter Count Error Message

Question by Guest | Last update on 2021-05-13 | Created on 2015-07-07

I would like to use the function IFNULL in MySQL in order to check whether a field in my table is NULL.

Up to now, I have tried the following query:

SELECT IFNULL(col, 0, col) FROM tab

This should return the content of "col" whenever "col" is not NULL or 0 if "col" is NULL.

However, I always get an error message stating that I have used a wrong number of parameters when calling the function:

Incorrect parameter count in the call to native function 'IFNULL'

What am I doing wrong? How should I call IFNULL instead? Does someone have any idea?

ReplyPositiveNegative
1Best Answer1 Vote

IFNULL expect 2 parameters, however, you are passing 3 parameters.

The first parameter for IFNULL is the value that should be checked. This value is returned if it is not NULL. However, if it is NULL, instead the second parameter will be returned.

So, your query should look like this:

SELECT IFNULL(col, 0) FROM tab

For clarification, here are some more examples for using IFNULL:

IFNULL(1, 0)     -- 1
IFNULL(NULL, 0)  -- 0
IFNULL('', 0)    -- ''

IFNULL(1, 0) returns 1 - because the first parameter is not NULL, the first parameter will be returned. IFNULL(NULL, 0) returns 0 - the first parameter is NULL, so the second parameter will be taking into account.  IFNULL('', 0) returns '' - because '' is not NULL.

By the way, alternatively, you can also work with IF. You can find an example in the topic "Check for Null".
Last update on 2021-05-13 | Created on 2015-07-08

ReplyPositive Negative
Reply

Related Topics

MySQL: Check for NULL

Question | 1 Answer

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.