case Expression Results

case expression uses a series of alternative result expressions.

The series of alternative result expressions in the example below are: R1, R2, ..., Rn, which are specified by the then and else clauses.

The rules for determining the datatype of a case expression are based on the same rules that determine the datatype of a column in a union operation. For example:

case
          when search_condition1 then R1
          when search_condition2 then R2
          ...
          else Rn
     end

The datatypes of the result expressions R1, R2, ..., Rn are used to determine the overall datatype of case. The same rules that determine the datatype of a column of a union that specifies n tables, and has the expressions R1, R2, …, Rn as the ith column, also determine the datatype of a case expression. The datatype of case is determined in the same manner as by the following query:

select...R1...from ...
union
select...R2...from...
union...
...
select...Rn...from...

Not all datatypes are compatible, and if you specify two datatypes that are incompatible (for example, char and int), your Transact-SQL query fails. See the Reference Manual: Building Blocks.