Sql Server2000不支持boolean型,怎么编IIF函数?特急

jzd1997 2003-08-01 02:49:36
Sql Server2000不支持Boolean型,我怎么编IIF函数,那位老大知道?望解答
我用如下代码编了个IIF函数
ALTER function IIF(@Jyouken sql_variant,@value1 sql_variant,@value2 sql_variant)
returns sql_variant
as
begin
declare @Keka sql_variant
-- print @Jyouken
if (cast(@Jyouken as int)=0)
set @Keka= @value2 --False
else
set @Keka= @value1 --True
return @Keka
end
用如下形式访问的时候老是出错select dbo.iif(nowyear is null,2000,nowyear) from Master,但是如果用如下方式就是正确的dbo.iif(nowyear,2000,2003)
...全文
184 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
Liyuet 2003-08-02
  • 打赏
  • 举报
回复



Numeric
IIf(«Logical Expression», «Numeric Expression1», «Numeric Expression2»)

如果 «Logical Expression» 得出 TRUE,這個函數傳回 «Numeric Expression1»;否則傳回 «Numeric Expression2»。

String
IIf(«Logical Expression», «String Expression1», «String Expression2»)

如果 «Logical Expression» 得出 TRUE,這個函數傳回 «String Expression1»;否則傳回 «String Expression2»。

HenanBoy 2003-08-02
  • 打赏
  • 举报
回复
大力就是大力每次就是这么多的代码,你是什么意思,不给思路,让我们按照思路作那,你这种做法,只能增加我们的依赖性
pengdali 2003-08-01
  • 打赏
  • 举报
回复
示例
A. 使用带有简单 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)

B. 使用带有简单 CASE 函数和 CASE 搜索函数的 SELECT 语句
在 SELECT 语句中,CASE 搜索函数允许根据比较值在结果集内对值进行替换。下面的示例根据图书的价格范围将价格(money 列)显示为文本注释。

USE pubs
GO
SELECT '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)

C. 使用带有 SUBSTRING 和 SELECT 的 CASE 函数
下面的示例使用 CASE 和 THEN 生成一个有关作者、图书标识号和每个作者所著图书类型的列表。

USE pubs
SELECT 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'
END
FROM 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)

zjcxc 2003-08-01
  • 打赏
  • 举报
回复
举个例子,下面的语句判断一个变量是否可以转换为数字型:

declare @a varchar(10)
set @a='123'
select case when isnumeric(@a)=1 then '数字' else '非数字' end


isnumeric()就是测试函数,它的返回值是bit类型
有两种取值:0对应boolean的false, 1对应boolean的true
pengdali 2003-08-01
  • 打赏
  • 举报
回复
select dbo.iif(case when nowyear is null then 1 else 0 end,2000,nowyear)
zjcxc 2003-08-01
  • 打赏
  • 举报
回复
SQL SERVER中

用bit代替了原来的boolean类型

用case when 代替了原来的iif
sdhdy 2003-08-01
  • 打赏
  • 举报
回复
try:
--bit是一种数据类型,值为0或1
ALTER function IIF(@Jyouken bit,@value1 int,@value2 int)
returns int
as
begin
declare @Keka int
if (@Jyouken =0)
set @Keka= @value2 --False
else
set @Keka= @value1 --True
return @Keka
end
97866 2003-08-01
  • 打赏
  • 举报
回复
bit 类型 :P
CrazyFor 2003-08-01
  • 打赏
  • 举报
回复
BIT类型就是你要的boolean型.
愉快的登山者 2003-08-01
  • 打赏
  • 举报
回复
使用BIT类型。
yehuazi 2003-08-01
  • 打赏
  • 举报
回复
dbo.iif(nowyear is null,2000,nowyear)
改成
dbo.iif(nowyear,2000,nowyear)

22,300

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧