Recently come across yet another SQL statement that required me to check if the denomiator is 0, otherwise the statement returns a devide by zero error.
Naturally, if this was the statement:
SELECT Expression1/Expression2 FROM Table1
You would do something like:
SELECT Expression1/(CASE Expression2=0 THEN XX ELSE Expression2 END) FROM Table1
This might be OK if Expression2 is small, however a better way I discovered is to use a combination of ISNULL and NULLIF
Then it would look like:
SELECT Expression1/ ISNULL( NULLIF(Expression2,0), XX) FROM Table1
Note: you can set the XX to be either NULL or 1 or something else depending on the application of this statement.