Link to home
Get AccessLog in
Avatar of mburk1968
mburk1968Flag for United States of America

asked on

Divide by Zero Error in SQL

I have the following code snippet that is throwing a divide by zero error. How do I resolve this and more importantly can I identify the record or records that is causing the issue?

        ( ( CASE WHEN D.line_status = 'I'
                 THEN ( D.price * D.total_qty
                        - ROUND({FN IFNULL(R.disc_perc, 0)} * D.price
                                * D.total_qty / 100, 4) )
                 ELSE 0
            END ) * D.co_rate ) AS SFC_TOTAL_INV_EXT_B ,
        ( (CASE WHEN D.line_status = 'I' THEN D.total_qty
                ELSE 0
           END) ) AS SFS_TOTAL_INV ,

Open in new window

Avatar of Sean Stuber
Sean Stuber

the only division in that snippet is dividing by 100


I don't think that snippet is where your error is happening.
Avatar of mburk1968

ASKER

Wrong snippet
        --( (( CASE WHEN D.line_status = 'I'
        --          THEN ( D.price * D.total_qty
        --                 - ROUND({FN IFNULL(R.disc_perc, 0)} * D.price
        --                         * D.total_qty / 100, 4) )
        --          ELSE 0
        --     END ) / ( CASE WHEN D.line_status = 'I' THEN D.total_qty
        --                    ELSE 0
        --               END )) ) AS UDF_ZZOODRPB_098 ,

Open in new window

It will happen when D.line_status is not equal to 'I' or when D.total_qty is zero.
In each case
( CASE WHEN D.line_status = 'I' THEN D.total_qty
        --                    ELSE 0
        --               END )

will return 0 to the divisor, which is not a valid operation.
ASKER CERTIFIED SOLUTION
Avatar of Sean Stuber
Sean Stuber

Link to home
membership
This content is only available to members.
To access this content, you must be a member of Experts Exchange.
Get Access
SOLUTION
Link to home
membership
This content is only available to members.
To access this content, you must be a member of Experts Exchange.
Get Access
d.line_status is null    would also drop you into your ELSE clause
Duh! Thank you both. Don't know where my head was at this morning.