Simple way to prevent a Divide By Zero error in SQL

前端 未结 5 1244
陌清茗
陌清茗 2021-02-05 17:15

I have a SQL query which used to cause a

Divide By Zero exception

I\'ve wrapped it in a CASE statement to stop this fr

5条回答
  •  灰色年华
    2021-02-05 17:43

    I'm using NULLIF bit differently, because in some cases I do have to return some value. Usually I need to return 0 when there is a divide by zero error. In that case I wrap whole expression in ISNULL. So it would be:

    Percentage =  ISNULL(100 * ClubTotal / NULLIF(AttTotal, 0), 0)
    

    The inner part is evaluated to NULL and then ISNULL replaces it with 0.

提交回复
热议问题