34,590
社区成员
发帖
与我相关
我的任务
分享
--> liangCK小梁 于2008-08-20
--> 生成测试数据: #table_1
if object_id('tempdb.dbo.#table_1') is not null drop table #table_1
create table #table_1 (字段1 int,字段2 int)
insert into #table_1
select 2,1 union all
select 2,null union all
select 2,0
select 字段1,字段2,字段1/nullif(字段2,0) as 比率 from #table_1 order by 比率
/*
字段1 字段2 比率
----------- ----------- -----------
2 NULL NULL
2 0 NULL
2 1 2
(3 行受影响)
*/
select 字段1,字段2,
case 字段2 when 0 then null else 字段1/字段2 end as 比率
from table_1 order by 比率
select 字段1,字段2,字段1/nullif(字段2,0) as 比率 from table_1 order by 比率