The IF function evaluates testCondition. If the condition is true, the function returns the value of valueTrue. Otherwise, it returns the value of valueFalseOrNull.
Syntax
IF(<testCondition>, <valueTrue>, <valueFalseOrNull>)
-- Standard example.
-- Returns 200.
SELECT IF(1 = 2, 100, 200);Parameters
testCondition: Required. The BOOLEAN expression to evaluate.
valueTrue: Required. The value to return if the testCondition expression evaluates to True.
valueFalseOrNull: Required. The value to return if the testCondition expression evaluates to FALSE or NULL.
The data types of valueTrue and valueFalseOrNull must be the same.
Return value
The return value has the same data type as the valueTrue or valueFalseOrNull parameter.
Examples
Example 1: Basic conditional check.
-- Returns 200.
SELECT IF(1=2, 100, 200);
-- Returns 'active'.
SELECT IF(LENGTH('abc') > 2, 'active', 'inactive');Example 2: If testCondition is NULL, it is treated as FALSE. The function then returns the value of valueFalseOrNull.
-- Returns 'valueB'.
SELECT IF(NULL, 'valueA', 'valueB');
-- Returns NULL.
SELECT IF(FALSE, 42, NULL);Related functions
The IF function is an Other function. For more information about functions for different business scenarios, see Other functions.