Updated on 2025-10-23 GMT+08:00

Process Control Functions

IF

IF(expr1, expr2, expr3)

Description: If expr1 is set to TRUE (that is, expr1 is not equal to 0 and expr1 is not NULL), IF() returns expr2. Otherwise, IF() returns expr3.

Examples:

m_db=# SELECT IF(true, 'ab', 1);
 case 
------
 ab
(1 row)

m_db=# SELECT IF(1>2, 1, NULL);
 case 
------

(1 row)

m_db=# SELECT IF(NULL, 'TRUE', 'FALSE');
 case  
-------
 FALSE
(1 row)

IFNULL

IFNULL(expr1, expr2)

Description: If expr1 is not null, IFNULL() returns expr1. If expr1 is null, IFNULL() returns expr2.

Examples:

m_db=# select ifnull(12, 123);
 ifnull 
--------
     12
(1 row)

m_db=# select ifnull(null, 123);
 ifnull 
--------
    123
(1 row)

NULLIF

NULLIF(expr1, expr2)

Description: Returns NULL if expr1 is equal to expr2; otherwise, returns expr1.

Examples:

m_db=# select nullif(123, 123);
 nullif 
--------

(1 row)

m_db=# select nullif(12, 123);
 nullif 
--------
     12
(1 row)