w3resource logo


MySQL ifnull() function

MySQL IFNULL() function

rating Average rating 7 out of 10. Total 48 users rated.

<<PreviousNext>>

Description

MySQL IFNULL() takes two expressions and if the first expression is not NULL, it returns the first expression. Otherwise it returns the second expression.

Depending on the context in which it is used, it returns either numeric or string value.


Syntax

IFNULL(expression1, expression2);

Arguments

Name Description
expression1 An expression.
expression2 An expression.

MySQL Version : 5.6

Example : MySQL IFNULL() function

The following MySQL statement returns the first expression, i.e. 0, since the first expression is not NULL.

SELECT IFNULL(0,2);

Output

MYSQl IFNULL

Example : IFNULL() function with non zero 1st argument

The following MySQL statement returns the first expression, i.e. 1, since the first expression is not NULL.

SELECT IFNULL(1,2);

Output

MYSQ IFNULL EXAMPLE

Example : IFNULL() function NULL

The following MySQL statement returns the second expression, i.e. 2, since the first expression is NULL.

SELECT IFNULL(NULL,2);

Output

MYSQl IFNULL EXAMPLE1



We have compiled a number of MySQL Questions and Answers. Check and assess you MySQL skill by visiting those questions and answers.

<<PreviousNext>>

Rate this tutorial


Your Rating: not set

Share this tutorial

RSS Feed