Case(expression, value1, result1, value2, result2, …, valueN, resultN, else-result)

This function checks if an expression is equal to multiple values and returns the corresponding result; otherwise, the else-result is returned.

Parameters

expression
The column, expression, or literal value to test. Can be of any type.
value
The column, expression, or literal value to compare with the expression. All values should be of the same type as the expression.
result
The column, expression, or literal value. Must all be of the same type as result1.
else-result
The column, expression, or literal value. Must be of the same type as result1. If omitted, it is assumed to be null.

Returns

The return type is the same as result1.

Remarks

The Case function is designed to avoid duplication of an expression when it is to be checked against a series of values. The Case function may contain an unlimited number of value-result pairs. The expression is evaluated once and compared to each of the values sequentially. If the expression is equal to any value, the corresponding result is returned. If it is not equal to any of the values, the else-result is returned.

Examples