CASE() Function : CASE « Function « SQL / MySQL






CASE() Function

     
mysql>
The CASE() function supports two slightly different formats.
mysql>
The first of these is shown in the following syntax:
mysql>
CASE WHEN <expression> THEN <result>
[{WHEN <expression> THEN <result>}...]
[ELSE <result>]
END
mysql>
mysql>
mysql> SELECT CASE WHEN 10*2=30 THEN '30 correct'
    -> WHEN 10*2=40 THEN '40 correct'
    -> ELSE 'Should be 10*2=20'
    -> END;
+-------------------------------------------------------------------------------------------------+
| CASE WHEN 10*2=30 THEN '30 correct'
WHEN 10*2=40 THEN '40 correct'
ELSE 'Should be 10*2=20'
END |
+-------------------------------------------------------------------------------------------------+
| Should be 10*2=20                                                                               |
+-------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

   
    
    
    
    
  








Related examples in the same category

1.The next version of the CASE() function
2.CASE Branching
3.CASE is a variant of IF that is useful when all the branch decisions depend on the value of a single expression.
4.Using case in select statement
5.Use both case expressions shown previously in a SELECT statement.
6.Using the comparison value form of CASE...WHEN
7.Using CASE...WHEN for evaluating conditions
8.Variants of CASE.WHEN with no ELSE clause
9.Use a CASE...WHEN function to indicate that the different categories have different sales trends
10.Case without else clause
11.Nested case statement
12.Case with comparison
13.Case when with and operator
14.Provide more meaningful value with case clause
15.Make more complete value with case statement
16.Using case statement to check a range
17.Use case else to check the exceptions
18.Using case with comparison
19.Case clause and aggregate function
20.If ELSE is omitted, the null value is returned.