case expression simplifies standard SQL expressions by allowing you to express a search condition using a when...then construct instead of an if statement. It supports conditional SQL expressions; can be used anywhere a value expression can be used.
case and expression syntax:
case when search_condition then expression [when search_condition then expression]... [else expression] end
case and value syntax:
case value when value then expression [when value then expression]... [else expression] end
select au_lname, postalcode, case when postalcode = "94705" then "Berkeley Author" when postalcode = "94609" then "Oakland Author" when postalcode = "94612" then "Oakland Author" when postalcode = "97330" then "Corvallis Author" end from authors
select stor_id, discount, coalesce (lowqty, highqty) from discounts
Yuo can also use the following format to produce the same result, since coalesce is an abbreviated form of a case expression:
select stor_id, discount, case when lowqty is not NULL then lowqty else highqty end from discounts
select title, nullif(type, "UNDECIDED") from titles
You can also use the following format to produce the same result, since nullif is an abbreviated form of a case expression:
select title, case when type = "UNDECIDED" then NULL else type end from titles
select price, coalesce (NULL, NULL, NULL) from titles
All result expressions in a CASE expression must not be NULL.
select stor_id, discount, coalesce (highqty) from discounts
A single coalesce element is illegal in a COALESCE expression.
update employees set salary = case dept when 'Video' then salary * 1.1 when 'Music' then salary * 1.2 else 0 end
select title, case movie_type when 1 then 'Horror' when 2 then 'Comedy' when 3 then 'Romance' when 4 then 'Western' else null end, our_cost from movie_titles
Use case with value when comparing values, where value is the value desired. If value equals expression, then the value of the case is result. If value1 does not equal express, valuet is compared to value2. If value equals value2, then the value of the CASE is result2. If none of the value1 ... valuen are equal to the desired valuet, then the value of the CASE is resultx. All of the resulti can be either a value expression or the keyword NULL. All of the valuei must be comparable types, and all of the results must have comparable datatypes.
If your query produces a variety of datatypes, the datatype of a case expression result is determined by datatype hierarchy. If you specify two datatypes that the SAP ASE server cannot implicitly convert (for example, char and int), the query fails.
See also if...else, select, where clause in Reference Manual: Commands.
ANSI SQL – Compliance level: Transact-SQL extension.
Any user can execute case.