Solved

T-SQL UDF - Divide By Zero

Posted on 2006-07-07
4
765 Views
Last Modified: 2008-03-03
Hi,

Having an issue with the Divide By Zero error whilst calculating a value in a SQL UDF.  Tried using the NULLIF and COALESCE functions, but can't seem to get them in the right context to stop the Divide By Zero.  Anyone help me out on this?  Thx.

Calculation:

@C32 = 0.0195
@C29 = 59
@C28 = 59
@C33 = 0.0193
@AnnualRealReturnStart = 0.039
@C16 = 0.0383

Set @C38 = Round( ((Power((1 + @C32) , (@C29 - @C28)) - 1) / @C33) / ((Power((1 + @AnnualRealReturnStart) , (@C29 - @C28)) - 1) / @C16) ,5)

becomes...

Set @C38 = Round( ((Power((1.0195) , 0)) - 1) / 0.0193) / ((Power((1.039) , 0)) - 1) / 0.0383) ,5)
0
Comment
Question by:simon_kirk
4 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 17056737

declare @ONE numeric(10,4)
set @ONE = 1

Set @C38 = Round( ((Power((@ONE + @C32) , (@C29 - @C28)) - @ONE) / @C33) / ((Power((@ONE+ @AnnualRealReturnStart) , (@C29 - @C28)) - @ONE) / @C16) ,5)
0
 
LVL 14

Expert Comment

by:mherchl
ID: 17056782
Set @C38 = case when ((Power((1 + @AnnualRealReturnStart) , (@C29 - @C28)) - 1) / @C16) <> 0 then Round( ((Power((1 + @C32) , (@C29 - @C28)) - 1) / @C33) / ((Power((1 + @AnnualRealReturnStart) , (@C29 - @C28)) - 1) / @C16) ,5) else 0 end
0
 
LVL 14

Author Comment

by:simon_kirk
ID: 17056954
angelIII, that revised calculation works great, and now returns a NULL value rather than the error, but I'm intrigued as to why changing the 1 to a variable assigned the value of 1 stops the divide by zero error.
0
 
LVL 1

Expert Comment

by:SnyTek
ID: 17057104
Create another UDF called "DevByZeroSaver" which accepts one parameter.
The function returns 1 if 0 was given and otherwise return the given number.
Makes it more readable but it could have an impact on performance depending on your data amount.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now