IFNULL( )

This MySQL function returns the results of the condition given in the first argument of the function if its results are not NULL. If the condition results are NULL, the results of the expression or string given in the second argument are returned.

Syntax

Below is the syntax for this command. Text within square brackets (i.e, [ and ] ) are optional. Choices are separated by bars (i.e, | ). Ellipses preceded by a comma indicates a repeating pattern. Ellipses before or after syntax are used to highlight a relevant except from the larger syntax. Text displayed in italic letters represent text that should be replaced with the specific names related to the database (e.g., column with the name of the column).

IFNULL(condition, result)

Explanation

This MySQL function returns the results of the condition given in the first argument of the function if its results are not NULL. If the condition results are NULL, the results of the expression or string given in the second argument are returned. It will return a numeric or a string value depending on the context.

Examples

SELECT CONCAT(name_first, SPACE(1), name_last) AS Client,
telephone_home AS Telephone,
IFNULL(goals, 'No Goals Given') AS Goals
FROM clients LIMIT 2;

+----------------+-----------+----------------+
| Client         | Telephone | Goals          |
+----------------+-----------+----------------+
| Janice Sogard  | 835-1821  | No Goals Given |
| Kenneth Bilich | 488-3325  | Long Term      |
+----------------+-----------+----------------+

This MySQL statement example provides a list of clients and their telephone numbers, along with their investment goals. If the client never told the broker of an investment goal (i.e., the goals column is NULL), the text No Goals Given is displayed.