【发布时间】:2016-09-23 10:33:28
【问题描述】:
我正在编写一个查询并得到一个除以零的错误。我之前使用单独的查询遇到过这个错误,但通常可以使用 NULLIF 语句修复它。
这一次我真的很难摆脱 NULLIF 的错误,我不确定这是不是因为我把它放在了正确的地方。
这是我的代码,没有任何 NULLIF 语句:
Select
DATEPART(Year,C.CreatedDate),
DATENAME(Month,C.CreatedDate),
CASE WHEN D.UnitID = 'BOX' THEN
D.QtyAffected * I.BCT_Product_Qty / (1 / (convert(decimal(18,3),T.Width) / 1000 * convert(decimal(18,3),T.Height) / 1000))
ELSE
D.QtyAffected / (1 / (convert(decimal(18,3),T.Width) / 1000 * convert(decimal(18,3),T.Height) / 1000))
End as 'M2 Affected',
C.CostReplacementTile + C.CostRedelivery + C.CostCredit + C.CostRefixing + C.CostVoucher 'Cost'
From BCTComplaintRegister C
Left Join BCTComplaintProductRegister D
On D.CaseID = C.CaseID
Left Join InventTable I
On I.ItemID = D.ItemID
Left Join BCTTileSize T
On T.TileSizeID = I.BCTTileSize
Where
--DATEDIFF(M,C.CreatedDate,GETDATE()) <= 1
C.CaseID = 'CE23294'
除以零错误与2个case语句有关,如果它不能执行除法,我只想返回零或其他东西。
谁能帮我把NULLIF放在哪里?或者有没有更好的选择。
谢谢
【问题讨论】: