CASE
计算条件列表并返回多个可能结果表达式之一。
CASE 具有两种格式:
简单 CASE 函数将某个表达式与一组简单表达式进行比较以确定结果。
CASE 搜索函数计算一组布尔表达式以确定结果。
两种格式都支持可选的 ELSE 参数。
语法
简单 CASE 函数:
CASE input_expression
CASE 搜索函数:
CASE
参数
input_expression
是使用简单 CASE 格式时所计算的表达式。Input_expression 是任何有效的 Microsoft® SQL Server™ 表达式。
WHEN when_expression
使用简单 CASE 格式时 input_expression 所比较的简单表达式。When_expression 是任意有效的 SQL Server 表达式。Input_expression 和每个 when_expression 的数据类型必须相同,或者是隐性转换。
n
占位符,表明可以使用多个 WHEN when_expression THEN result_expression 子句或 WHEN Boolean_expression THEN result_expression 子句。
THEN result_expression
当 input_expression = when_expression 取值为 TRUE,或者 Boolean_expression 取值为 TRUE 时返回的表达式。result expression 是任意有效的 SQL Server 表达式。
ELSE else_result_expression
当比较运算取值不为 TRUE 时返回的表达式。如果省略此参数并且比较运算取值不为 TRUE,CASE 将返回 NULL 值。Else_result_expression 是任意有效的 SQL Server 表达式。Else_result_expression 和所有 result_expression 的数据类型必须相同,或者必须是隐性转换。
WHEN Boolean_expression
使用 CASE 搜索格式时所计算的布尔表达式。Boolean_expression 是任意有效的布尔表达式。
结果类型
从 result_expressions 和可选 else_result_expression 的类型集合中返回最高的优先规则类型。有关更多信息,请参见数据类型的优先顺序。
结果值
简单 CASE 函数:
计算 input_expression,然后按指定顺序对每个 WHEN 子句的 input_expression = when_expression 进行计算。
返回第一个取值为 TRUE 的 (input_expression = when_expression) 的 result_expression。
如果没有取值为 TRUE 的 input_expression = when_expression,则当指定 ELSE 子句时 SQL Server 将返回 else_result_expression;若没有指定 ELSE 子句,则返回 NULL 值。
CASE 搜索函数:
按指定顺序为每个 WHEN 子句的 Boolean_expression 求值。
返回第一个取值为 TRUE 的 Boolean_expression 的 result_expression。
如果没有取值为 TRUE 的 Boolean_expression,则当指定 ELSE 子句时 SQL Server 将返回 else_result_expression;若没有指定 ELSE 子句,则返回 NULL 值。
示例
A. 使用带有简单 CASE 函数的 SELECT 语句
在 SELECT 语句中,简单 CASE 函数仅检查是否相等,而不进行其它比较。下面的示例使用 CASE 函数更改图书分类显示,以使其更易于理解。
USE pubs
GO
SELECT
FROM titles
WHERE price IS NOT NULL
ORDER BY type, price
COMPUTE AVG(price) BY type
GO
下面是结果集:
Category
------------------- ------------------------- --------------------------
Business
Business
Business
Business
Category
------------------- ------------------------- --------------------------
Modern Cooking
Modern Cooking
Category
------------------- ------------------------- --------------------------
Popular Computing
Popular Computing
Category
------------------- ------------------------- --------------------------
Psychology
Psychology
Psychology
Psychology
Psychology
Category
------------------- ------------------------- --------------------------
Traditional Cooking Fifty Years in Buckingham 11.95
Traditional Cooking Sushi, Anyone?
Traditional Cooking Onions, Leeks, and Garlic 20.95
(21 row(s) affected)
B. 使用带有简单 CASE 函数和 CASE 搜索函数的 SELECT 语句
在 SELECT 语句中,CASE 搜索函数允许根据比较值在结果集内对值进行替换。下面的示例根据图书的价格范围将价格(money 列)显示为文本注释。
USE pubs
GO
SELECT
FROM titles
ORDER BY price
GO
下面是结果集:
Price Category
--------------------- --------------------
Not yet priced
Not yet priced
Very Reasonable Title The Gourmet Microwav
Very Reasonable Title You Can Combat Compu
Very Reasonable Title Life Without Fear
Very Reasonable Title Emotional Security:
Coffee Table Title
Coffee Table Title
Coffee Table Title
Coffee Table Title
Coffee Table Title
Coffee Table Title
Coffee Table Title
Coffee Table Title
Expensive book!
Expensive book!
Expensive book!
Expensive book!
(18 row(s) affected)
C. 使用带有 SUBSTRING 和 SELECT 的 CASE 函数
下面的示例使用 CASE 和 THEN 生成一个有关作者、图书标识号和每个作者所著图书类型的列表。
USE pubs
SELECT SUBSTRING((RTRIM(a.au_fname) + ' '+
FROM titleauthor ta JOIN authors a ON ta.au_id = a.au_id
下面是结果集:
Name
------------------------- ----------- -------- -------------------
Johnson White
Marjorie Green
Marjorie Green
Cheryl Carson
Michael O'Leary
Michael O'Leary
Dean Straight
Abraham Bennet
Ann Dull
Burt Gringlesby
Charlene Locksley
Charlene Locksley
Reginald Blotchet-Halls
Akiko Yokomoto
Innes del Castillo
Michel DeFrance
Stearns MacFeather
Stearns MacFeather
Livia Karsen
Sylvia Panteley
Sheryl Hunter
Anne Ringer
Anne Ringer
Albert Ringer
Albert Ringer
(25 row(s) affected)