All Products
Search
Document Center

:IF

Last Updated:Oct 17, 2023

The IF function is used to select a statement to execute based on a specific condition. This topic describes how to use the IF function in Hologres.

Limits

Only Hologres V2.1 and later support the IF function. If the version of your Hologres instance is V2.0 or earlier, upgrade your Hologres instance. For more information, see Instance upgrades.

Syntax

IF(condition, value_if_true, value_if_false)

The following table describes the parameters in the preceding syntax.

Parameter

Description

condition

The condition.

value_if_true

If the result of the condition is TRUE, the result that is specified by value in this statement is returned.

value_if_false

If the result of the condition is FALSE or NULL, the result that is specified by value in this statement is returned.

Examples

DROP TABLE IF EXISTS if_test;
CREATE TABLE if_test (
    id int,
    name text,
    age int
);

INSERT INTO if_test VALUES ('1', 'a', '18'), ('2', 'b', '19'), ('3', 'c', '25'), ('4', 'd', '8'), ('5', 'e', '27'), ('6', 'f', '21');

SELECT
    name,
    IF (age >= 18, 'Adult', 'Minor')
FROM
    if_test;

The following result is returned:

+-------+---------+
| name  | if      |
+-------+---------+
| a     |  Adult   |
| c     |  Adult   |
| e     |  Adult   |
| f     |  Adult   |
| d     |  Minor |
| b     |  Adult   |