简体   繁体   English

如何处理 SQL 查询中的除以零错误

[英]How to handle Divide by zero Error in SQL query

Running following sql query in sql server.在 sql server 中运行以下 sql 查询。

Select id, description, cost, price, ((price-cost)/price) as Margin
from item

I am getting this error message.我收到此错误消息。

Msg 8134, Level 1, State 1, Line 1
Divide by zero error encountered.

Values in price and cost are more than zero even after substraction they are more than zero.价格和成本的值即使在减去它们大于零后也大于零。 Don't understand why I am getting error message.不明白为什么我收到错误消息。

使用nullif(price,0)

Select id, description, cost, price, (price-cost)/nullif(price,0) as Margin from item

try using NULL Functions尝试使用 NULL 函数
use NULIF(price,0)使用 NULIF(price,0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM