Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Computed Columns & Divide By Zero Errors Expand / Collapse
Author
Message
Posted Monday, January 14, 2013 6:00 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, January 23, 2013 7:03 AM
Points: 158, Visits: 239
Hi,

I have some computed columns in a table that under some circumstances fail due to a divide by zero error. Now, I know I can just use regular columns and do the required calculations with appropriate logic to avoid any divide by zero errors and achieve the result I want. But, is there a way of maintaining the computed columns and avoid such errors?

Regards

Steve
Post #1406678
Posted Monday, January 14, 2013 6:15 AM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Saturday, August 23, 2014 6:03 AM
Points: 351, Visits: 1,556
For clarity, can you share your example schema definition, along with test data indicating the desired output please?

One suggestion would be to handle the case for 0 within your computed column definition and assigning NULL in it's place.

create table testTable
(
ValueOne int,
ValueTwo int,
Calcuation as ValueOne/case when ValueTwo = 0 then null else ValueTwo end
)

insert into testTable(ValueOne,ValueTwo) values(1,1)
insert into testTable(ValueOne,ValueTwo) values(1,0)

select * from testTable




John Sansom (@sqlBrit) | www.johnsansom.com
Post #1406683
Posted Monday, January 14, 2013 6:22 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Tuesday, September 16, 2014 2:20 AM
Points: 5,231, Visits: 5,106
So the computed column is ColA / ColB, what do you want the output to be.

If ColA is 10 and ColB is 0, do you want the computation to be 10 or 0

For 0
ISNULL(ColA / NULLIF(ColB, 0), 0)

For 10
ColA / ISNULL(NULLIF(ColB,0), 1)




Want an answer fast? Try here
How to post data/code for the best help - Jeff Moden
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
CrossTabs-Part1 & Part2 - Jeff Moden
SQL Server Backup, Integrity Check, and Index and Statistics Maintenance - Ola Hallengren
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger

Post #1406684
Posted Monday, January 14, 2013 7:34 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, January 23, 2013 7:03 AM
Points: 158, Visits: 239
Looks as if your examples provide the answers I need.

Thanks :)
Post #1406726
Posted Monday, January 14, 2013 7:38 AM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Saturday, August 23, 2014 6:03 AM
Points: 351, Visits: 1,556
raotor (1/14/2013)
Looks as if your examples provide the answers I need.

Thanks :)

You're welcome, glad we could help.



John Sansom (@sqlBrit) | www.johnsansom.com
Post #1406731
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse