Update with calculation

0

I have a product table and I want to calculate the profit margin based on the columns preco_venda and preco_custo done as below:

update produto set margem_lucro=((((preco_unit/preco_custo)*100)-100))

Error occurs

  

ERROR: division by zero

     

ERROR: division by zero SQL state: 22012

What's wrong?

    
asked by anonymous 25.03.2017 / 18:18

1 answer

1

In some line the value of cost_cost must be zero, can not be divided by zero, do:

update produto set margem_lucro =
                   (case when preco_produto > 0 then 
                              ((((preco_unit/preco_custo) * 100) - 100))         
                         else null end)
    
25.03.2017 / 21:11