none
Divide by zero error encountered RRS feed

  • General discussion

  • cast((([totalAR]-[totalAP])/(totalAR))*100 as DECIMAL(10,2)) AS 'Gross margin %',

    This is my equation and I get the [S0001][8134] Divide by zero error encountered

    I tried this but it still didn't work

    nullif(cast((([totalAR]-[totalAP])/(totalAR))*100 as DECIMAL(10,2)),0) AS 'Gross margin %',

    Any ideas?

    Monday, June 19, 2017 8:05 PM

All replies

  • It's the divide by that's at issue - so somewhere along the line totalAR is 0 so put the nullif on this part of your equation

    /nullif(totalAR,0)

    Monday, June 19, 2017 8:19 PM
  • What about this?

    nullif(cast((([totalAR]-[totalAP])/(case when totalAR=0 then totalAP else totalAR end))*100 as DECIMAL(10,2)),0) AS 'Gross margin %',

    This will only work if TotalAP and TotalAR are never both 0 together.

    Monday, June 19, 2017 9:07 PM