遇到以零作除数错误

yikun 2008-08-20 02:49:17
table_1
字段1 ,字段2
2 1
2 null
2 0

select 字段1,字段2,字段1/字段2 as 比率 from table_1 order by 比率

显示遇到以零作除数错误

我希望出来的结果是

2 1 2
2 null null
2 0 null

请问怎么写?
...全文
366 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
shenlu198806 2011-06-06
  • 打赏
  • 举报
回复
楼上高明
qiaoqihua 2008-08-20
  • 打赏
  • 举报
回复
select 字段1,字段2,CASE 字段2 WHEN 0 THEN NULL ELSE 字段1/字段2 END as 比率 from table_1 order by 比率
水族杰纶 2008-08-20
  • 打赏
  • 举报
回复
case when end
liangCK 2008-08-20
  • 打赏
  • 举报
回复
--> 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 行受影响)
*/
arrow_gx 2008-08-20
  • 打赏
  • 举报
回复
select 字段1,字段2,
case 字段2 when 0 then null else 字段1/字段2 end as 比率
from table_1 order by 比率
liangCK 2008-08-20
  • 打赏
  • 举报
回复
select 字段1,字段2,字段1/nullif(字段2,0) as 比率 from table_1 order by 比率 

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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