you're reading...
SQL Server

Computed Columns and Divide by Zero Errors

Lets say you have a query, computed column etc. that has the following formula: SomeColumnA / SomeColumnB, where SomeColumnA and SomeColumnB are non-NULL numeric columns.  Clearly a problem can arise if there is a record in the database with SomeColumnB equal to 0.0, as that will result in a Divide by zero error.

Thankfully there is a rather simple solution:

use NULLIF to see if SomeColumnB was 0.0.  If so,  have it return NULL, and the division will result in NULL.  Then wrapped the whole thing in an ISNULL, so if the division resulted in NULL, the result was 0.0.

As an alternate solution use the case statement


SomeColumnB = 0 then 0.0

ELSE SomeColumnA / SomeColumnB





About ldgaller

Accomplished Data Warehouse Architect, DBA and Software Architect with over 15 years of professional experience and demonstrated success designing and implementing solutions that improve business functionality and productivity. Highly diverse technical background with proven ability to design, develop and implement technology on an enterprise level. I approach all projects with passion, diligence, integrity, and exceptional aptitude.


No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: