mysql - SQL Divide By Zero Error In SELECT Statement -


i'm brand new sql , use hand. i'm getting divide 0 error following in select statement:

sum(cast((replace(replace(replace (p.[total sales], '$', ''), '(','-'),  ')','')) money)) - sum(cast((replace(replace(replace (p.[total cost], '$', ''), '(','-'),  ')','')) money)) / sum(cast((replace(replace(replace (p.[total sales], '$', ''), '(','-'),  ')','')) money)) new_bal 

i know need use case, i'm not sure of application.

you use nullif in following:

nullif(expression1, 0)

in case be:

sum(cast((replace(replace(replace (p.[total sales], '$', ''), '(','-'),  ')','')) money)) - sum(cast((replace(replace(replace (p.[total cost], '$', ''), '(','-'),  ')','')) money)) / nullif(sum(cast((replace(replace(replace (p.[total sales], '$', ''), '(','-'),  ')','')) money)),0) new_bal 

Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -