Link to home
Start Free TrialLog in
Avatar of Fordraiders
FordraidersFlag for United States of America

asked on

calculate price markup percentage divide by zero error

calculate price markup percentage
divided by zero error.

,[NetCostPerUnit] - [SellingPricePerUnit] / [NetCostPerUnit]  * 100 as MarkupPercentage2

I'm running this calculation
but keep getting a error "divided by zero error."

Thanks
fordraiders
Avatar of Daniel Pineault
Daniel Pineault

Take a look at https://stackoverflow.com/questions/861778/how-to-avoid-the-divide-by-zero-error-in-sql

[NetCostPerUnit] - COALESCE([SellingPricePerUnit] / NULLIF([NetCostPerUnit],0), 0) * 100 as MarkupPercentage2

Open in new window

make sure the value of [NetCostPerUnit] is not zero?

try this instead:

case when [NetCostPerUnit] = 0 then 0 else
[NetCostPerUnit] - [SellingPricePerUnit] / [NetCostPerUnit]  * 100 end as MarkupPercentage2

Open in new window

Avatar of Fordraiders

ASKER

ok using  NetCostPerUnit  as  183.66   and  SellingPricePerUnit  =  245.00

the markup percentage should be  33.40  I keep coming up with a negative number.
-33.40

 ([NetCostPerUnit] - [SellingPricePerUnit]) / nullif([NetCostPerUnit],0) * 100  as MarkupPercentage2
ASKER CERTIFIED SOLUTION
Avatar of Daniel Pineault
Daniel Pineault

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Daniel, Thanks and  i need to keep the data at 2 decimals  ??
Can that be done here ?

,([SellingPricePerUnit] -[NetCostPerUnit] ) / nullif([NetCostPerUnit],0) * 100  as MarkupPercentage
ok, got it
cast(([SellingPricePerUnit] -[NetCostPerUnit]) / nullif([NetCostPerUnit],0) * 100 as decimal(18,2))  as MarkupPercentage2
Thanks all !!