good example below on how to use the NULLIF() function
here

select sum((xtndprce-extcost)/NULLIF(xtndprce,0)) as ActualMargin

I used in a group by select