被零整除的问题

zhangns 2009-05-15 03:36:11
表中有两个字段A和B
需要在视图中显示A/B的值
但B可能为0,当B为0时,就不计算,直接显示为0

该怎样做?
...全文
258 27 打赏 收藏 转发到动态 举报
写回复
用AI写文章
27 条回复
切换为时间正序
请发表友善的回复…
发表回复
rmini 2009-05-18
  • 打赏
  • 举报
回复
select id,id2,case id2 when 0 then 0 else cast(id as decimal)/id2 end
from tab
drop table tab
zhaoweiting0609 2009-05-18
  • 打赏
  • 举报
回复
mark
Liyingyue_FFS 2009-05-18
  • 打赏
  • 举报
回复
case when B=0 then 0 else A/B end
soft_wsx 2009-05-18
  • 打赏
  • 举报
回复
set ansi_warnings  off
set arithabort off
--设置这两项就可以了
  • 打赏
  • 举报
回复
注意三点:
1、除数为零要过了掉,此时使用case判断;
2、如果除数被除数都是整数时,要注意首先转化为其他类型,否则结果就取整了;
3、是否需要考虑四舍五入也是要注意的。
  • 打赏
  • 举报
回复

(3 行受影响)
id id2
----------- ----------- ---------------------------------------
1 2 0.50000000000
3 9 0.33333333333
5 0 0.00000000000

(3 行受影响)
  • 打赏
  • 举报
回复
create table tab(id int,id2 int)
insert tab
select 1,2 union
select 3,9 union
select 5,0
select id,id2,case id2 when 0 then 0 else cast(id as decimal)/id2 end
from tab
drop table tab
wanshichen 2009-05-16
  • 打赏
  • 举报
回复
case when B=0 then 0 else A/B end
lihan6415151528 2009-05-16
  • 打赏
  • 举报
回复

case when B=0 then 0 else A/B end
qqshenyunzcz 2009-05-16
  • 打赏
  • 举报
回复
视图不会自动处理吗?
msdnlu 2009-05-16
  • 打赏
  • 举报
回复
CASE解决问题
  • 打赏
  • 举报
回复
case when B=0 then 0 else A/B end
JonasFeng 2009-05-15
  • 打赏
  • 举报
回复
感觉是很简单的问题呀。
pl_mm 2009-05-15
  • 打赏
  • 举报
回复

--设置会话选项
set ansi_warnings off
set arithabort off

select isnull(5/0,0)


0
sugar2009 2009-05-15
  • 打赏
  • 举报
回复
select CASE WHEN B=0 THEN 0 ELSE A/B END FROM 表
jjoulejcc 2009-05-15
  • 打赏
  • 举报
回复
case
yanleiyigan 2009-05-15
  • 打赏
  • 举报
回复
o.o
claro 2009-05-15
  • 打赏
  • 举报
回复
-狙击手- 2009-05-15
  • 打赏
  • 举报
回复
。。
加载更多回复(7)

34,590

社区成员

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

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