Search code examples
sqlsql-server-2008nulldivide-by-zero

SQL NULLIF Function not working around divisor


I am trying to use a NULLIF function to return a NULL value where the divisor of my query results in zero and therefore returns a divide by zero error. However, I'm having trouble wrapping the function around my statement. The statement contains a CAST, CASE and SUM function. I've wrapped the function around the divisor in the example below, but this doesn't work and I've tried other combinations.

cast(
  round(
      cast(
          sum(
              case 
                  when @StuYear=11 AND [Levels of Progress] < 3 then 
                      1 
              when @StuYear=10 AND [Levels of Progress] < 2 then 
                      1
              when @StuYear=9 AND [Levels of Progress] < 1 then 
                      1
          else 
                      0 
              end) as decimal)
/
NULLIF(
cast(
    sum(
        case
            when [Levels of Progress] is NULL then 
                0 
            else 
                1 
        end) as decimal) * 100,1) as numeric(4,1))
,0)

Solution

  • The syntax you posted is invalid and hard to read, also it seems your logic is wrong.

    Try this instead:

    declare @stuyear int = 11
    select
      cast(
        sum(
          case when @StuYear=11 and [Levels of Progress] < 3 or 
                    @StuYear=10 and [Levels of Progress] < 2 or
                    @StuYear=9  and [Levels of Progress] < 1 then 1
             else 0 end
         )*100.0 /
        NULLIF(count([Levels of Progress]),0)
      as Numeric(5,2))
    from (values (cast(null as int))) x([Levels of Progress])
    

    Replace the from part with your own table. This is a valid syntax returns null when the count is null.