Training
Module
Branch the flow of code using the switch-case construct in C# - Training
Learn how to add branching logic that matches one variable or expression against many possible values.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
case
expressionApplies to: Databricks SQL Databricks Runtime
Returns resN
for the first optN
that equals expr
or def
if none matches.
Returns resN
for the first condN
evaluating to true, or def
if none found.
CASE expr {WHEN opt1 THEN res1} [...] [ELSE def] END
CASE {WHEN cond1 THEN res1} [...] [ELSE def] END
expr
: Any expression for which comparison is defined.optN
: An expression that has a least common type with expr
and all other optN
.resN
: Any expression that has a least common type with all other resN
and def
.def
: An optional expression that has a least common type with all resN
.condN
: A BOOLEAN expression.The result type matches the least common type of resN
and def
.
If def
is omitted the default is NULL.
Conditions are evaluated in order and only the resN
or def
which yields the result is executed.
> SELECT CASE WHEN 1 > 0 THEN 1 WHEN 2 > 0 THEN 2.0 ELSE 1.2 END;
1.0
> SELECT CASE WHEN 1 < 0 THEN 1 WHEN 2 > 0 THEN 2.0 ELSE 1.2 END;
2.0
> SELECT CASE WHEN 1 < 0 THEN 1 WHEN 2 < 0 THEN 2.0 END;
NULL
> SELECT CASE 3 WHEN 1 THEN 'A' WHEN 2 THEN 'B' WHEN 3 THEN 'C' END;
C
Training
Module
Branch the flow of code using the switch-case construct in C# - Training
Learn how to add branching logic that matches one variable or expression against many possible values.
Documentation
if function - Azure Databricks - Databricks SQL
Learn the syntax of the if function of the SQL language in Databricks SQL and Databricks Runtime.
Built-in functions - Azure Databricks - Databricks SQL
Learn about built-in functions in Databricks SQL and Databricks Runtime.
coalesce function - Azure Databricks - Databricks SQL
Learn the syntax of the coalesce function of the SQL language in Databricks SQL and Databricks Runtime.