SQL Tutorial – IFNULL() function in MySQL

The IFNULL() in SQL is basically used to replace null values with some other values. This function takes two arguments –

IFNULL(expression, alt_value)

expression – The expression that you want test

alt_value – The value to return if expression is NULL

If you run the following code –


SELECT IFNULL(5, 0) -- return 5
SELECT IFNULL('LWD', 0) -- return LWD string

The first query will return 5 as 5 is not a null and the second query will return the string LWD as this is also not null.

But if the first expression is NULL

SELECT IFNULL(NULL, 0) -- return 0

Then it will return the second argument that is passed to the IFNULL function which is 0 in this case.

Rating: 1 out of 5.

Leave a Reply