sql server - How to protect sql statement from Divide By Zero error -


i'm in process of creating reports take finite total (lets 2,500) of products (for example lets ice cream cones) , counts how many of them broken before serving.

now actual count code of broken cones i've got down.

select count(broken_cones) [ice].[ice_cream_inventory]  broken_cones = 'yes' 

however, need percentage of broken cones total well. i've been playing around code keep running 'divide zero' error code below.

select cast(nullif((.01 * 2500)/count(broken_cones), 0)  decimal(7,4)) [ice].[ice_cream_inventory] broken_cones = 'yes' 

for right now, there aren't broken cones (and won't while) total right zero. how can show null scenario zero?

i tried place isnull statement in mix kept getting 'divide zero' error. doing right?

::edit::

here's ended with.

select  case when count(broken_cones) = 0 0 else cast(nullif((.01 * 2500)/count(broken_cones), 0) decimal(7,4)) end [ice].[ice_cream_inventory] broken_cones = 'yes' 

use case statement.

select  case when count(broken_cones) = 0 0 else cast(nullif((.01 * 2500)/count(broken_cones), 0) decimal(7,4)) end [ice].[ice_cream_inventory] broken_cones = 'yes' 

Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -