CASE (simple)
Simple CASE returns a result based on the value of a single input expression, or a default result if none of the comparison values match.
See also: IF.
Sample usage
Replace payment codes with intuitive names:
CASE Payment Type
WHEN "CC" THEN "Credit Card"
WHEN "DC" THEN "Debit Card"
WHEN "GC" THEN "Gift Card"
WHEN "CA" THEN "Cash"
ELSE "Other"
END
Syntax
CASE input_expression
WHEN expression_to_match THEN result
[WHEN expression_to_match THEN result]
[...]
[ELSE else_result]
END
Parameters
input_expression- Any valid field or expression.- [
expression_to_match- Any valid field or expression. TheWHENclause comparesinput_expressiontoinput_expressionand returns true if the two are equal, or false if they aren't.]{#when-conditions} result- Any valid field or expression. EachWHENclause must have a matchingTHENclause, which specifies the results to return if that condition is true. If there are multipleWHENclauses, theCASEstatement returns the result for the first true clause.else_result(optional) - Any valid field or expression. TheELSEelse_result clause specifies a default result for theCASEstatement. This clause is returned if none of theWHENclauses are true. If aCASEstatement has noELSEclause, and none of theWHENclauses are true, theCASEstatement returnsNULL.
How simple CASE works
A simple CASE statement consists of the following elements:
- The
CASEkeyword, followed by an input expression. WHEN: the value against which to compare theinput_expression: if the value equals theinput_expression, then this clause is true. You can have multipleWHENclauses in a singleCASEstatement.THEN: the result to return if theWHENclause's condition is true. You must have oneTHENclause for eachWHENclause in yourCASEstatement.ELSE: Optional. If none of theWHENclause conditions are true,CASEreturns the value in theELSEclause, orNULLif noELSEclause is specified.- The
ENDkeyword.
CASE evaluates each successive WHEN clause and returns the first result where the condition is true. Any remaining WHEN clauses and the ELSE result are not evaluated. If all WHEN conditions are false or NULL, CASE returns the ELSE result, or if no ELSE clause is present, returns NULL.
Example
Provide customized links for your premium customers:
CASE Premium Status
WHEN "Platinum" THEN CONCAT(Site URL, "platinum_welcome.html")
WHEN "Gold" THEN CONCAT(Site URL, "gold_welcome.html")
WHEN "Silver" THEN CONCAT(Site URL, "silver_welcome.html")
ELSE CONCAT(Site URL, "welcome.html")
END