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 1234»»»

Computed Column Divide by Zero? Expand / Collapse
Author
Message
Posted Wednesday, October 13, 2010 9:50 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Sunday, August 3, 2014 3:41 PM
Points: 1,293, Visits: 1,430
Comments posted to this topic are about the item Computed Column Divide by Zero?
Post #1004103
Posted Wednesday, October 13, 2010 9:55 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 5:50 AM
Points: 1,117, Visits: 1,375
Good question.
If computed columns are persisted in nature (e.g. QTot AS QF1 + QF2 PERSISTED,) then you receive the error after "Insert Zeroes" print statement and "SELECT * FROM @Quantities" statement returns 2 rows - datasource Test1 & Test3.


Thanks
Post #1004104
Posted Wednesday, October 13, 2010 10:56 PM
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Thursday, January 2, 2014 9:57 AM
Points: 554, Visits: 863
Thanks For the Good Question Tom Brown i learnt some thing new today and also i tried to create index on the computed columns it will not alllow to create index directly.

and i tried in this way


 

PRINT 'Define Table';
CREATE TABLE #Quantities (
ID INT IDENTITY,
DataSource varchar(30) NOT NULL,
QF1 Float,
QF2 Float,
QTot AS QF1 + QF2,
PF1 AS QF1 / (QF1 + QF2),
PF2 AS QF2 / (QF1 + QF2) );

CREATE NONCLUSTERED INDEX IXC1 on #Quantities(ID) INCLUDE(PF1,PF2)

PRINT 'Insert data';
INSERT INTO #Quantities (DataSource, QF1, QF2)
SELECT 'Test 1' , 66, 34;

Print 'Insert Zeroes';
INSERT INTO #Quantities (DataSource, QF1, QF2)
SELECT 'Test 2', 0, 0;

Print 'Insert Nulls';
INSERT INTO #Quantities (DataSource, QF1, QF2)
SELECT 'Test 3', null, null;

PRINT 'Show Table';
SELECT * FROM #Quantities;

DROP TABLE #Quantities



i got the error while inserting the value it self

thanks again for the Good question
Post #1004120
Posted Wednesday, October 13, 2010 11:38 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Monday, July 21, 2014 3:43 AM
Points: 1,938, Visits: 1,162
Lost two points because of misunderstanding of set arthiabort on.I already worked divide by zero errors and handle these type of errors.but little bit confused.with arthabort you can handle this other wise using nullif also we can handle.Againg thanks for good question on basics.

Malleswarareddy
I.T.Analyst
MCITP(70-451)
Post #1004134
Posted Wednesday, October 13, 2010 11:44 PM
UDP Broadcaster

UDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP Broadcaster

Group: General Forum Members
Last Login: Sunday, June 29, 2014 11:26 PM
Points: 1,481, Visits: 1,960
Great question.

However... why does SQL let something get stored (and im not talking about the computed column itself since that aint stored... unless its persisted) in the database in the first case, that it later cant read? Anyone know the reasoning behind that? (Besides performance that is)

/T
Post #1004135
Posted Wednesday, October 13, 2010 11:58 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Wednesday, August 13, 2014 8:07 AM
Points: 1,366, Visits: 989
In order to get round the divide by zero error, I generally do this:

PF1 AS QF1 / nullif((QF1 + QF2),0),
instead of
PF1 AS QF1 / (QF1 + QF2),

I.e. set the divisor to null, if the divisor is zero.
I used to do complicated Case statements, but then I needed to retype the divisor.
This way, I just add "NULLIF(" at the front and ",0)" at the end.

I'we written about it at Stack overflow

Best regards,
Henrik Staun Poulsen
www.stovi.com




Post #1004142
Posted Thursday, October 14, 2010 12:38 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Tuesday, August 26, 2014 4:51 AM
Points: 1,854, Visits: 3,451
Stupid me, lost the points because I was thinking of persisted computed columns. Don't know why.
Post #1004150
Posted Thursday, October 14, 2010 12:52 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 4:28 PM
Points: 5,984, Visits: 8,242
I totally agree with the other comments: good question. I wanted to add a comment about the effect of persisting the computed column (by adding the PERSISTED option or by including it in an index), but others have beaten me to it.

tommyh (10/13/2010)
why does SQL let something get stored (and im not talking about the computed column itself since that aint stored... unless its persisted) in the database in the first case, that it later cant read? Anyone know the reasoning behind that? (Besides performance that is)

The developer chose not to persist the computed column. That means that the developer required SQL Server not to evaluate the expression until referenced. The only way for SQL Server to "know" that the expression can't be evaluated is by trying to evaulate it, so there is no way to tell when the data is stored if there will be an error - except if SQL Server chooses to disregard the developer's wish.
Further, it might also be the case that the data stored is intermediate. The first stage of processing an order might involve copying the data from the order form; a later stage adds other data. A column that is only needed after the second stage could be a computed column that results in an error before the second stage is completed. Think about a computed column that lists the time between receipt of an order and shipping of the goods - while not producing an error, the computation would not result in a meaningful value if querie while the order is still being processed.

henrik staun poulsen (10/13/2010)
In order to get round the divide by zero error, I generally do this:

PF1 AS QF1 / nullif((QF1 + QF2),0),
instead of
PF1 AS QF1 / (QF1 + QF2),

I.e. set the divisor to null, if the divisor is zero.
I used to do complicated Case statements, but then I needed to retype the divisor.
This way, I just add "NULLIF(" at the front and ",0)" at the end.

That would be my suggestion as well. While the suggestion in the explanation works as well, it requires the person who queries the data to know that the column is computed, AND to know the exact expression. Writing the expression so that there can never be an error encapsulates this knowledge and reduces the risk of errors when new people start writing queries.
BTW, the NULLIF function is simply a shorthand for the complicated CASE expressions you used to do. The NULLIF reduces the amount of code to maintain, but does not offer any performance benefits.



Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Post #1004151
Posted Thursday, October 14, 2010 1:24 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: 2 days ago @ 1:23 AM
Points: 2,509, Visits: 2,386
henrik staun poulsen (10/13/2010)
In order to get round the divide by zero error, I generally do this:

PF1 AS QF1 / nullif((QF1 + QF2),0),
instead of
PF1 AS QF1 / (QF1 + QF2),

Good. The computed column should be NULL, if expression is invalid.
Post #1004159
Posted Thursday, October 14, 2010 2:02 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 12:41 AM
Points: 3,930, Visits: 5,119
good question.
thanks


____________________________________________
Space, the final frontier? not any more...
All limits henceforth are self-imposed.
“libera tute vulgaris ex”
Post #1004167
« Prev Topic | Next Topic »

Add to briefcase 1234»»»

Permissions Expand / Collapse