Sql server encountered met to divide by zero error

Two ways:

Original: SELECT (Quantity * Price) / (Quantity * UnitPrice) AS [the average] FROM ## Table error: encountered to divide by zero error

1, where the divisor is determined in the denominator is 0 (a 'NULLIF' function)

 SELECT (Quantity * Price) / NULLIF ((Quantity * UnitPrice), 0) AS [average] FROM ## table 

 

2, where the divisor is determined in the denominator is 0 (a 'CASE WHEN' function)

 SELECT (CASE WHEN Quantity*UnitPrice=0 THEN 0 ELSE (Quantity*Price)/(Quantity*UnitPrice)) AS [平均值]   FROM ##表 

Guess you like

Origin www.cnblogs.com/lydg/p/11362373.html