content_image/691987.jpg content_image/692006.jpg
<img src='content_image/691987.jpg'>

• CASE value  WHEN compare_value  THEN result  [WHEN compare_value  THEN result ...] [ELSE result ] END 

CASE WHEN condition  THEN result  [WHEN condition  THEN result  ...] [ELSE result ] END 

The first CASE  syntax returns the result  for the first value = compare_value  comparison that is true. The second syntax returns the result for the first condition that is true. If no comparison or condition is true, the result after ELSE  is returned, or NULL  if there is no ELSE  part. 

<img src='content_image/692006.jpg'>

## Note 

The syntax of the CASE operator  described here differs slightly from that of the SQL CASE statement  described in Section 13.6.5.1, “CASE Statement” , for use inside stored programs. The CASE  statement cannot have an ELSE NULL  clause, and it is terminated with END CASE  instead of END . 

The return type of a CASE  expression result is the aggregated type of all result values: 

•If all types are numeric, the aggregated type is also numeric: 

•If at least one argument is double precision, the result is double precision. 

•Otherwise, if at least one argument is DECIMAL , the result is DECIMAL . 

•Otherwise, the result is an integer type (with one exception): 

•If all integer types are all signed or all unsigned, the result is the same sign and the precision is the highest of all specified integer types (that is, TINYINT , SMALLINT , MEDIUMINT , INT , or BIGINT ). 

•If there is a combination of signed and unsigned integer types, the result is signed and the precision may be higher. For example, if the types are signed INT  and unsigned INT , the result is signed BIGINT . 

•The exception is unsigned BIGINT  combined with any signed integer type. The result is DECIMAL with sufficient precision and scale 0. 

•If all types are BIT , the result is BIT . Otherwise, BIT  arguments are treated similar to BIGINT . 

•If all types are YEAR , the result is YEAR . Otherwise, YEAR  arguments are treated similar to INT . 

•If all types are character string ( CHAR  or VARCHAR ), the result is VARCHAR  with maximum length determined by the longest character length of the operands. 

•If all types are character or binary string, the result is VARBINARY . 

