ID EN
Flow Control Functions

IFNULL(expr1,expr2)

MySQL 8.4 🇮🇩 Bahasa Indonesia

Jika expr1 bukan NULL, IFNULL() mengembalikan expr1; jika tidak, ia akan mengembalikan expr2.

Syntax

MYSQL
SELECT IFNULL(1,0);
1
SELECT IFNULL(NULL,10);
10
SELECT IFNULL(1/0,10);
10
SELECT IFNULL(1/0,'yes');
'yes'

Contoh

Example

The default return type of IFNULL(expr1,expr2) is the more “general” of the two expressions, in the order STRING, REAL, or INTEGER. Consider the case of a table based on expressions or where MySQL must internally store a value returned by IFNULL() in a temporary table:

MYSQL
SELECT IFNULL(1,0);
1
SELECT IFNULL(NULL,10);
10
SELECT IFNULL(1/0,10);
10
SELECT IFNULL(1/0,'yes');
'yes'
Example 2

In this example, the type of the test column is VARBINARY(4) (a string type).

MYSQL
CREATE TABLE tmp SELECT IFNULL(1,'test') AS test;
DESCRIBE tmp;
+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| test  | varbinary(4) | NO   |     |         |       |
+-------+--------------+------+-----+---------+-------+