Updated on 2026-11-06 GMT+08:00

Conditional Functions

Overview

Conditional functions return different values based on the condition expression. CASE WHEN supports multi-condition branch logic, IF implements simple conditional value selection, COALESCE is used to replace null values, NULLIF is used to set equivalent values to null, ISNULL/ISNOTNULL is used to determine null values, and GREATEST/LEAST is used to select extreme values from multiple values. AI DataLake supports all conditional functions.

For more information, see Conditional Functions.

Application Scenarios

  • Data classification: CASE WHEN is used to map continuous values to discrete categories. For example, amounts are classified into high, medium, and low tiers.
  • Conditional value selection: The IF function is used to quickly select between two values. For example, different status texts are returned based on the flag.
  • Null value replacement: COALESCE is used to replace NULL with a default value to prevent NULL from participating in calculations and causing the result to be NULL.
  • Equality setting to null: NULLIF is used to return NULL when two values are equal. A typical use case is to avoid division by zero errors.
  • Null value judgment: ISNULL or ISNOTNULL in WHERE conditions or CASE WHEN is used to determine whether a value is NULL.
  • Extreme value selection: GREATEST or LEAST is used to select the maximum or minimum value from multiple columns or expressions, automatically skipping NULL.

Notes and Constraints

  • The return types of all WHEN/THEN and ELSE must be compatible. The system automatically derives the common type. If the ELSE clause is missing and no condition is met, NULL is returned.
  • The types of true_value and false_value must be compatible. The system automatically derives the common type. The condition must be of the BOOLEAN type.
  • At least two parameters are required. All parameter types must be compatible, and the return type is the common type of all parameters.
  • The types of value1 and value2 must be comparable. NULL is returned only when value1 = value2 is TRUE. If the comparison result is NULL, value1 is returned.
  • At least two parameters are required. All parameter types must be compatible and sortable. NULL values are automatically skipped. If all values are NULL, NULL is returned.

Syntax Structure

Syntax

Return Type

Description

Example

CASE WHEN condition1 THEN result1 [WHEN condition2 THEN result2]* [ELSE result] END

Consistent with result

Search condition. The conditions are evaluated in sequence, and the first result that meets the condition (condition is TRUE) is returned. If none of the conditions is met and the ELSE clause exists, the ELSE value is returned. If none of the conditions is met and the ELSE clause does not exist, NULL is returned. The condition must be of the BOOLEAN type. All result types must be compatible.

SELECT CASE WHEN 1 > 2 THEN 'a' WHEN 2 > 1 THEN 'b' ELSE 'c' END; -- 'b'

CASE value WHEN value1 THEN result1 [WHEN value2 THEN result2]* [ELSE result] END

Consistent with result

Value matching condition. The value is compared with each valueN following WHEN in sequence, and the first matching result is returned. If none of the values is matched and the ELSE clause exists, the ELSE value is returned. If none of the values is matched and the ELSE clause does not exist, NULL is returned. The value and each valueN must be of the comparable type. All result types must be compatible.

SELECT CASE 2 WHEN 1 THEN 'one' WHEN 2 THEN 'two' ELSE 'other' END; -- 'two'

IF(condition, true_value, false_value)

Consistent with the common type of true_value and false_value.

Conditional value function. Returns true_value if condition is TRUE, and returns false_value if condition is FALSE. The condition must be of the BOOLEAN type. The types of true_value and false_value must be compatible. If condition is NULL, false_value is returned.

SELECT IF(1 > 2, 'a', 'b'); -- 'b'

COALESCE(value1, value2, ...)

Consistent with the first non-null value

Returns the first non-null value in the parameter list. At least two parameters are required. All parameter types must be compatible. If all parameters are NULL, NULL is returned.

SELECT COALESCE(NULL, NULL, 'default'); -- 'default'

NULLIF(value1, value2)

Consistent with value1 or NULL

Null-if function. Returns NULL if value1 is equal to value2; otherwise, returns value1. The types of value1 and value2 must be comparable. This function is often used to avoid division-by-zero errors (for example, amount / NULLIF(count, 0)).

SELECT NULLIF(5, 5); -- NULL

ISNULL(value)

BOOLEAN

Determines whether a value is null. Returns TRUE if value is NULL, and returns FALSE otherwise. This function is equivalent to value IS NULL and is not affected by the rule that NULL is returned when NULL participates in a general comparison.

SELECT ISNULL(NULL); -- TRUE

ISNOTNULL(value)

BOOLEAN

Determines whether a value is not null. If the value is not NULL, TRUE is returned. Otherwise, FALSE is returned. This function is equivalent to value IS NOT NULL and is the logical negation of ISNULL.

SELECT ISNOTNULL(1); -- TRUE

GREATEST(value1, value2, ...)

Consistent with the common type of the parameters.

Returns the maximum value in the parameter list, automatically skipping NULL. At least two parameters are required. All parameter types must be compatible and sortable. If all values are NULL, NULL is returned.

SELECT GREATEST(3, 1, 4, 1, 5); -- 5

LEAST(value1, value2, ...)

Consistent with the common type of the parameters.

Returns the minimum value in the parameter list, automatically skipping NULL. At least two parameters are required. All parameter types must be compatible and sortable. If all values are NULL, NULL is returned.

SELECT LEAST(3, 1, 4, 1, 5); -- 1