DECODE

functionsupportedCore SQL functions

docs.snowflake.com ↗

Conditional expression functions

Compares the select expression to each search expression in order. As soon as a search expression matches the selection expression, the corresponding result expression is returned. Note DECODE in Snowflake is different from the DECODE function in PostgreSQL, which converts data into different encodings.

Syntax

supportedpartialnot supportednot scored

DECODE( <expr> , <search1> , <result1> [ , <search2> , <result2> ... ] [ , <default> ] )

Parameters

NameRequiredDescription
exprrequiredThis is the “select expression”. The “search expressions” are compared to this select expression, and if there is a match then DECODE returns the result that corresponds to that search expression. The select expression is typically a column, but can be a subquery, literal, or other expression.
searchNrequiredThe search expressions indicate the values to compare to the select expression. If one of these search expressions matches, the function returns the corresponding result. If more than one search expression would match, only the first match’s result is returned.
resultNrequiredThe results are the values that will be returned if one of the search expressions matches the select expression.
defaultrequiredIf an optional default is specified, and if none of the search expressions match the select expression, then DECODE returns this default value.

Usage notes

Collation details

- The collation specifications of the select expression and the search expressions must all be compatible. - The value returned from the function retains the collation specification of the result with the highest-precedence collation.