给你一个例子:
USE pubs
SELECT
CASE type
WHEN 'popular_comp' THEN 'Popular Computing'
WHEN 'mod_cook' THEN 'Modern Cooking'
WHEN 'business' THEN 'Business'
WHEN 'psychology' THEN 'Psychology'
WHEN 'trad_cook' THEN 'Traditional Cooking'
ELSE 'Not yet categorized'
END AS Category,
CONVERT(varchar(30), title) AS "Shortened Title",
price AS Price
FROM titles
WHERE price IS NOT NULL
ORDER BY 1
USE pubs
SELECT
CASE type
WHEN 'popular_comp' THEN 'Popular Computing'
WHEN 'mod_cook' THEN 'Modern Cooking'
WHEN 'business' THEN 'Business'
WHEN 'psychology' THEN 'Psychology'
WHEN 'trad_cook' THEN 'Traditional Cooking'
ELSE 'Not yet categorized'
END AS Category,
CONVERT(varchar(30), title) AS "Shortened Title",
price AS Price
FROM titles
WHERE price IS NOT NULL
ORDER BY 1
解决方案 »
- 求一条查询语句,9个表连接查询,大家帮个忙。
- 菜鸟在线等查询语句,万分感谢!
- |zyciis| 求一条简单的SQL Group By查询语句
- 随机写入10000个IP(不重复)
- SQL Server 2005 date类型数据修改问题
- 问一个简单存储过程问题
- 大家是否用过sql server 的 sql mail,它的主要用途………………
- ?SQL高手请进,一个如何区分全角半角的问题
- 用PB如何实现超长报表,"每页上"在显示和打印时都有最左端的项目列。
- 交叉表如何有条件的执行?即字符型变量@jsdm的引用方式
- 数据库的一个表有10000多点记录,但用select count(*) from table 时却不能执行
- 在公司的LAN中实现数据的管理、备份的功能,win98+sql 2K个人版够用了?
Evaluates a list of conditions and returns one of multiple possible result expressions. CASE has two formats: The simple CASE function compares an expression to a set of simple expressions to determine the result.
The searched CASE function evaluates a set of Boolean expressions to determine the result.
Both formats support an optional ELSE argument. Syntax
Simple CASE function:CASE input_expression
WHEN when_expression THEN result_expression
[...n]
[
ELSE else_result_expression
]
ENDSearched CASE function:CASE
WHEN Boolean_expression THEN result_expression
[...n]
[
ELSE else_result_expression
]
ENDArguments
input_expression
Is the expression that is evaluated when using the simple CASE format. input_expression is any valid Microsoft® SQL Server™ expression.
WHEN when_expression
Is a simple expression to which input_expression is compared when using the simple CASE format. when_expression is any valid SQL Server expression. The data types of input_expression and each when_expression must be the same or must be an implicit conversion.
n
Is a placeholder indicating that multiple WHEN when_expression THEN result_expression clauses, or multiple WHEN Boolean_expression THEN result_expression clauses can be used.
THEN result_expression
Is the expression that is returned when input_expression equals when_expression evaluates to TRUE, or Boolean_expression evaluates to TRUE. result expression is any valid SQL Server expression.
ELSE else_result_expression
Is the expression that is returned if no comparison operation evaluates to TRUE. If this argument is omitted and no comparison operation evaluates to TRUE, CASE returns NULL. else_result_expression is any valid SQL Server expression. The data types of else_result_expression and any result_expression must be the same or must be an implicit conversion.
WHEN Boolean_expression
Is the Boolean expression that is evaluated when using the searched CASE format. Boolean_expression is any valid Boolean expression.
if @aa=1
exec ..........
else
if @aa=2
exec ......
else
if @aa=3
exec........
请教!!