T-SQL UDF - Divide By Zero

Posted on 2006-07-07
Last Modified: 2008-03-03

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.


@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)


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

Accepted Solution

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)
LVL 14

Expert Comment

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
LVL 14

Author Comment

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.

Expert Comment

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.

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
I have a large data set and a SSIS package. How can I load this file in multi threading?
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

713 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