CASE

Translation reference to convert Transact-SQL Case expression to Snowflake Scripting

circle-info

Important Notice: Migration of Documentation Website

Please be advised that our documentation website is currently undergoing a migration to a new platform. To ensure you have access to the most up-to-date information, we kindly request that you visit our new documentation website located at:

Official Snowflake Snowconvert Documentationarrow-up-right

For any immediate assistance or if you encounter any issues, please contact our support team at [email protected]envelope.

Thank you for your understanding.

chevron-rightApplies tohashtag
circle-info

Some parts in the output code are omitted for clarity reasons.

Description

Transact-SQL has two possible formats of the Case expression. both of them for the purpose of evaluating expressions and conditionally obtaining results. The first one refers to a Simple Case Expression that will evaluate if an input_expression matches one or more of the when_expression. The second one will evaluate each Boolean_expression independently. The else clause is supported in both formats.

According to the official Transact-SQL Case documentation:

CASE can be used in any statement or clause that allows a valid expression. For example, you can use CASE in statements such as SELECT, UPDATE, DELETE and SET, and in clauses such as select_list, IN, WHERE, ORDER BY, and HAVING.

For more information regarding Transact-SQL Case, check herearrow-up-right.

-- Simple CASE expression:   
CASE input_expression   
     WHEN when_expression THEN result_expression [ ...n ]   
     [ ELSE else_result_expression ]   
END   

-- Searched CASE expression:  
CASE  
     WHEN boolean_expression THEN result_expression [ ...n ]   
     [ ELSE else_result_expression ]   
END

Note: Transact-SQL allows to optionally encapsulate the input_expression and the boolean_expression in parentheses; Snowflake Scripting too.

Sample Source Patterns

The following examples detail two scenarios where the Case expression can be used and their differences from Snowflake Scripting.

Select using Case

Transact-SQL

Snowflake Scripting

Note that in this scenario there are no differences regarding the Case expression itself.

circle-exclamation

Set using Case

The AdventureWorks2019 database was used in both languages to obtain the same results.

Transact-SQL

Snowflake Scripting

circle-exclamation

Known Issues

No issues were found.

  1. SSC-EWI-0073: Pending Functional Equivalence Review.

Last updated