IFNULL

IFNULL is a MySQL function used to replace NULL values with a specified replacement value.

IFNULL(expression, replace_with_expression)

  • expression: This is the first parameter of `IFNULL` function. It’s an expression that will be evaluated and returned if its value is NOT NULL.
  • replace_with_expression: This is the second parameter of the `IFNULL` function. If the initial expression evaluates to NULL, then this is the value or expression that will be returned instead.

Example

SELECT IFNULL(NULL, 'This is not null');

Output

'This is not null'

Explanation

The IFNULL function replaces NULL values with the specified replacement value. In this case, NULL is replaced with the string 'This is not null'.

IFNULL(expression_1, expression_2);

  • expression_1: This is the expression that is evaluated first by the IFNULL function. If the value of expression_1 is not NULL, the function will return this value.
  • expression_2: This is the alternative expression that the IFNULL function will return if expression_1 is NULL.

Example

SELECT IFNULL(name, 'No Name')
FROM employees WHERE id = 7;

Output

No Name

Explanation

The IFNULL function in SQLite checks the first expression if it is NULL. If it is NULL, the function returns the second expression. In this example, the IFNULL function checks if the ‘name’ field of the employee with the ‘id’ of 7 is NULL. If it is NULL, it returns ‘No Name’.

For in-depth explanations and examples SQL keywords where you write your SQL, install our extension.