|
|
CASE Expression
With a
CASEexpression, it is possible to specify a conditional value. Depending on the result of one or more conditional expressions, theCASEexpression can return different values.A
CASEexpression can be in one of the following two forms.CASE Expression First Form
Rules
The following rules apply to
CASEexpressions:
- If one or more
search-conditionsare true, then the result of theCASEexpression is the result of the first (left-most)WHENclause which has asearch-conditionthat is true.- If none of the
search-conditionsare true, then the result of theCASEexpression is the result of the explicit or implicitELSEclause.- If no
ELSEclause is specified thenELSE NULLis implicit.- At least one result in a
CASEexpression must express a value different fromNULL.See Result Data Types for a description of how the data type of the result of the
CASEexpression is determined.Example
CASE WHEN col1 < 10 THEN 1
WHEN col1 >= 10 THEN 2
ELSE 3
ENDCASE Expression Second Form
Rules
The following rules apply to
CASEexpressions:
- If no
ELSEclause is specified thenELSE NULLis implicit.- At least one result in a
CASEexpression must express a value different fromNULL.See Result Data Types for a description of how the data type of the result of the
CASEexpression is determined.Example
CASE col1 WHEN 0 THEN NULL WHEN -1 THEN -999 ELSE col1 ENDShort Forms for CASE
There are two short forms for special
CASEexpressions:NULLIFandCOALESCE.NULLIF
NULLIF(x1, x2)CASE WHEN x1 = x2 THEN NULL ELSE x1 ENDI.e. if the operands are equal, the
NULLIFexpression has the valueNULL, otherwise it has the value of the first operand.COALESCE
COALESCE(x1,x2)CASE WHEN x1 IS NOT NULL THEN x1 ELSE x2 ENDCOALESCE(x1,x2,...,xn)CASE WHEN x1 IS NOT NULL THEN x1 ELSE COALESCE(x2,...,xn) ENDI.e. the
COALESCEexpression returns the value of the first non-NULLoperand, found by working from left to right, orNULLif all the operands equalNULL.
|
Mimer Information Technology AB Voice: +46 18 780 92 00 Fax: +46 18 780 92 40 info@mimer.se |
|
|