IfThen/IIF/Case
来源:互联网 发布:淘宝网消保规则 编辑:程序博客网 时间:2024/06/04 18:21
功能:使用表达式返回两(多)个值之一.
Delphi(帮助文档):
Math单元:
[Delphi] function IfThen(AValue: Boolean, const ATrue: Integer, const AFalse: Integer): Integer;
[Delphi] function IfThen(AValue: Boolean, const ATrue: Int64, const AFalse: Int64): Int64;
[Delphi] function IfThen(AValue: Boolean, const ATrue: Double, const AFalse: Double): Double;
StrUtils单元:
[Delphi] function IfThen(AValue: Boolean, const ATrue: string, AFalse: string): string;
Access (http://office.microsoft.com/zh-cn/access/HA011161002052.aspx?pid=CL100570042052):
eg1:IIf([Confirmed] = "Yes", "Order Confirmed", "Order Not Confirmed")
使用 IIf 函数在 Confirmed 字段值为 Yes 时显示消息“Order Confirmed”;如果 Confirmed 字段值不为 Yes,那么显示消息“Order Not Confirmed”。
eg2:IIf(IsNull([Country]), " ", [Country])
使用 IIf 函数在 Country 字段值为 Null 时显示空字符串;如果 Country 字段值不为 Null,那么显示 Country 控件值。
eg3:=IIf(IsNull([Region]), [City] & " " & [PostalCode], [City] & " " & [Region] & " " & [PostalCode])
使用 IIf 函数在 Region 为 Null 时显示 City 和 PostalCode 字段的值;如果 Region 不为 Null,那么显示 City、Region 和 PostalCode 字段的值。
SQL Server (SQL Server 联机丛书):
egA. 使用带有简单 CASE 函数的 SELECT 语句
在 SELECT 语句中,简单 CASE 函数仅检查是否相等,而不进行其它比较。下面的示例使用 CASE 函数更改图书分类显示,以使其更易于理解。
USE pubs
GO
SELECT Category =
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,
CAST(title AS varchar(25)) AS 'Shortened Title',
price AS Price
FROM titles
WHERE price IS NOT NULL
ORDER BY type, price
COMPUTE AVG(price) BY type
GO
下面是结果集:
Category Shortened Title Price ------------------- ------------------------- -------------------------- Business You Can Combat Computer S 2.99 Business Cooking with Computers: S 11.95 Business The Busy Executive's Data 19.99 Business Straight Talk About Compu 19.99 avg ========================== 13.73 Category Shortened Title Price ------------------- ------------------------- -------------------------- Modern Cooking The Gourmet Microwave 2.99 Modern Cooking Silicon Valley Gastronomi 19.99 avg ========================== 11.49 Category Shortened Title Price ------------------- ------------------------- -------------------------- Popular Computing Secrets of Silicon Valley 20.00 Popular Computing But Is It User Friendly? 22.95 avg ========================== 21.48 Category Shortened Title Price ------------------- ------------------------- -------------------------- Psychology Life Without Fear 7.00 Psychology Emotional Security: A New 7.99 Psychology Is Anger the Enemy? 10.95 Psychology Prolonged Data Deprivatio 19.99 Psychology Computer Phobic AND Non-P 21.59 avg ========================== 13.50 Category Shortened Title Price ------------------- ------------------------- -------------------------- Traditional Cooking Fifty Years in Buckingham 11.95 Traditional Cooking Sushi, Anyone? 14.99 Traditional Cooking Onions, Leeks, and Garlic 20.95 avg ========================== 15.96 (21 row(s) affected)
egB. 使用带有简单 CASE 函数和 CASE 搜索函数的 SELECT 语句
在 SELECT 语句中,CASE 搜索函数允许根据比较值在结果集内对值进行替换。下面的示例根据图书的价格范围将价格(money 列)显示为文本注释。
USE pubsGOSELECT 'Price Category' = CASE WHEN price IS NULL THEN 'Not yet priced'
WHEN price < 10 THEN 'Very Reasonable Title'
WHEN price >= 10 and price < 20 THEN 'Coffee Table Title'
ELSE 'Expensive book!'
END,
CAST(title AS varchar(20)) AS 'Shortened Title'
FROM titles
ORDER BY price
GO
下面是结果集:
Price Category Shortened Title --------------------- -------------------- Not yet priced Net Etiquette Not yet priced The Psychology of Co 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 Is Anger the Enemy? Coffee Table Title Cooking with Compute Coffee Table Title Fifty Years in Bucki Coffee Table Title Sushi, Anyone? Coffee Table Title Prolonged Data Depri Coffee Table Title Silicon Valley Gastr Coffee Table Title Straight Talk About Coffee Table Title The Busy Executive's Expensive book! Secrets of Silicon V Expensive book! Onions, Leeks, and G Expensive book! Computer Phobic And Expensive book! But Is It User Frien (18 row(s) affected)
eg
C. 使用带有 SUBSTRING 和 SELECT 的 CASE 函数
下面的示例使用 CASE 和 THEN 生成一个有关作者、图书标识号和每个作者所著图书类型的列表。
USE pubsSELECT SUBSTRING((RTRIM(a.au_fname) + ' '+ RTRIM(a.au_lname) + ' '), 1, 25) AS Name, a.au_id, ta.title_id, Type = CASE WHEN SUBSTRING(ta.title_id, 1, 2) = 'BU' THEN 'Business' WHEN SUBSTRING(ta.title_id, 1, 2) = 'MC' THEN 'Modern Cooking' WHEN SUBSTRING(ta.title_id, 1, 2) = 'PC' THEN 'Popular Computing' WHEN SUBSTRING(ta.title_id, 1, 2) = 'PS' THEN 'Psychology' WHEN SUBSTRING(ta.title_id, 1, 2) = 'TC' THEN 'Traditional Cooking' ENDFROM titleauthor ta JOIN authors a ON ta.au_id = a.au_id
下面是结果集:
Name au_id title_id Type ------------------------- ----------- -------- ------------------- Johnson White 172-32-1176 PS3333 Psychology Marjorie Green 213-46-8915 BU1032 Business Marjorie Green 213-46-8915 BU2075 Business Cheryl Carson 238-95-7766 PC1035 Popular Computing Michael O'Leary 267-41-2394 BU1111 Business Michael O'Leary 267-41-2394 TC7777 Traditional Cooking Dean Straight 274-80-9391 BU7832 Business Abraham Bennet 409-56-7008 BU1032 Business Ann Dull 427-17-2319 PC8888 Popular Computing Burt Gringlesby 472-27-2349 TC7777 Traditional Cooking Charlene Locksley 486-29-1786 PC9999 Popular Computing Charlene Locksley 486-29-1786 PS7777 Psychology Reginald Blotchet-Halls 648-92-1872 TC4203 Traditional Cooking Akiko Yokomoto 672-71-3249 TC7777 Traditional Cooking Innes del Castillo 712-45-1867 MC2222 Modern Cooking Michel DeFrance 722-51-5454 MC3021 Modern Cooking Stearns MacFeather 724-80-9391 BU1111 Business Stearns MacFeather 724-80-9391 PS1372 Psychology Livia Karsen 756-30-7391 PS1372 Psychology Sylvia Panteley 807-91-6654 TC3218 Traditional Cooking Sheryl Hunter 846-92-7186 PC8888 Popular Computing Anne Ringer 899-46-2035 MC3021 Modern Cooking Anne Ringer 899-46-2035 PS2091 Psychology Albert Ringer 998-72-3567 PS2091 Psychology Albert Ringer 998-72-3567 PS2106 Psychology (25 row(s) affected)
- IfThen/IIF/Case
- MDX-IIF函数和CASE声明的执行计划
- SQL Server中 Case When 函数 与 Access中 IIF 函数的应用
- 3.2.2 ifthen
- 慎用IfThen函数!
- Delphi 中的 IfThen 函数
- Delphi 中的 IfThen 函数
- IIf (MDX)
- IIF函数
- IIf 函数
- IIF函数的使用
- access中的IIf函数
- 关于IIf函数
- decode实现iif
- IIf 函数的语法
- VB中的IIF() 函数
- sql 不能识别iif
- access IIf函数
- 为什么要始终使用PreparedStatement代替Statement?
- 久违
- 输入法杀手病毒
- 在VC中MSFlexGrid内嵌EDIT、COMBOBOX的实现方法
- javascript动态树形结构的实现
- IfThen/IIF/Case
- Apache 的简单Rewrite
- SQLServer如何改变数据库对象所有者?
- PHP基础知识总结
- javascript实现简易计算器
- QQ等级:QQ在线等级说明
- 一个数据库完全一致恢复的测试实例
- Window Mobile 手机检测环境噪声
- 久违这咖啡的味道