Home > OS >  Divide by zero error encountered with Decimals
Divide by zero error encountered with Decimals

Time:01-10

 ,(CAST(SUM(Y.[Profit]) AS DECIMAL (18,2)) / CAST(SUM(Y.[Sales]) AS DECIMAL (18,2))) AS 'AHT' 

Does anyone know where do I put ISNULL or NULLIF so that I wouldn't encounter a 'Divide by zero error encountered' error?

I expect to see NUll or Zero (0)

CodePudding user response:

You need to check divider before operation, something like :

CASE 
    WHEN SUM(Y.[Sales]) != 0
    THEN (CAST(SUM(Y.[Profit]) AS DECIMAL (18,2)) / CAST(SUM(Y.[Sales]) AS DECIMAL (18,2))) 
    ELSE NULL
END AS 'AHT'  
  • Related