You are viewing documentation about an older version (1.12.1). View latest version

snowflake.snowpark.CaseExpr

class snowflake.snowpark.CaseExpr(expr: CaseWhen)[source]

Bases: Column

Represents a CASE expression.

To construct this object for a CASE expression, call the functions.when() specifying a condition and the corresponding result for that condition. Then, call when() and otherwise() methods to specify additional conditions and results.

Examples:

>>> from snowflake.snowpark.functions import when, col, lit

>>> df = session.create_dataframe([[None], [1], [2]], schema=["a"])
>>> df.select(when(col("a").is_null(), lit(1)) \
...     .when(col("a") == 1, lit(2)) \
...     .otherwise(lit(3)).alias("case_when_column")).collect()
[Row(CASE_WHEN_COLUMN=1), Row(CASE_WHEN_COLUMN=2), Row(CASE_WHEN_COLUMN=3)]
Copy

Methods

else_(value)

Sets the default result for this CASE expression.

otherwise(value)

Sets the default result for this CASE expression.

when(condition, value)

Appends one more WHEN condition to the CASE expression.