SQLServerCentral.com / SQL Server 2005 / T-SQL (SS2K5) / Calculate XIRR in SQL Server / Latest PostsInstantForum.NET v2.9.0SQLServerCentral.comhttp://www.sqlservercentral.com/Forums/notifications@sqlservercentral.comFri, 29 Jul 2016 14:39:59 GMT20RE: Calculate XIRR in SQL Serverhttp://www.sqlservercentral.com/Forums/Topic919838-338-1.aspxI got it to work using a guess rate of -.9Tue, 22 Apr 2014 10:49:29 GMTkayla_wildeRE: Calculate XIRR in SQL Serverhttp://www.sqlservercentral.com/Forums/Topic919838-338-1.aspxI've been using this and it works great but it doesn't work if the XIRR is negative, did you ever modify this code to handle that?Tue, 22 Apr 2014 10:34:48 GMTkayla_wildeRE: Calculate XIRR in SQL Serverhttp://www.sqlservercentral.com/Forums/Topic919838-338-1.aspxThe Newton-Raphson approach to finding roots can also be done in an rCTE:[url=http://www.sqlservercentral.com/articles/T-SQL/90955/]Exploring Recursive CTEs by Example [/url]Mon, 17 Feb 2014 17:59:22 GMTdwain.cRE: Calculate XIRR in SQL Serverhttp://www.sqlservercentral.com/Forums/Topic919838-338-1.aspxAn XIRR is just a function name in Excel, it is actually an IRR calculation when the timings for the cash flows are given using a date schedule. This then leads to discounting of the cash flows at the actual time period as opposed to periodic time periods in IRR calculationThere are more than one way to define the XIRR equation and there are scores of ways of finding the roots of this equation that results in one or more IRR valuesMy personal favorite algo to find roots of a given function is the popular Newton-Raphson method that was named after the two mathematicians who independently discovered this method independent of each other about more than two centuries ago. One is the famous inventor of modern day Calculus the English mathematician Sir Isaac Newton (whom it seem was Knighted by the King of England thus the title "Sir" is referenced. The other one is Joseph Raphson, I am not sure of his national origin but it sounds like an American name so who knows I didn't look it upCode may not always return an XIRR in such a case use a Guess rate other than the last one tried. In other case a Division by zero may be encountered when the differential is zero.[code="sql"]CREATE TYPE dbo.MyXirrTable AS TABLE ( theValue DECIMAL(19, 9) NOT NULL, theDate DATETIME NOT NULL )GOCREATE FUNCTION dbo.XIRR(@Sample MyXirrTable READONLY,@Rate DECIMAL(19, 9) = 0.1)RETURNS DECIMAL(38, 9)ASBEGINDECLARE @X DECIMAL(19, 9) = 0.0, @X0 DECIMAL(19, 9) = 0.1, @f DECIMAL(19, 9) = 0.0, @fbar DECIMAL(19, 9) = 0.0, @i TINYINT = 0,IF @Rate IS NULL SET @Rate = 0.1SET @X0 = @RateWHILE @i < 100 BEGIN SELECT @f = 0.0, @fbar = 0.0 SELECT @f = @f + theValue * POWER(1 + @X0, (-theDelta / 365.0E)), @fbar = @fbar - theDelta / 365.0E * theValue * POWER(1 + @X0, (-theDelta / 365.0E - 1)) FROM ( SELECT theValue, DATEDIFF(DAY, MIN(theDate) OVER (), theDate) AS theDelta FROM @Sample ) AS d IF @fbar = 0 RETURN NULL SET @X = @X0 - @f / @fbar If ABS(@X - @X0) < 0.00000001 RETURN @X SET @X0 = @X SET @i += 1 END RETURN NULLENDGO[/code]The above code is based on financial functions found in this JavaScript library [url=https://github.com/FinancialEngineer/tadJS/]tadJS[/url] that itself is based upon [url=http://tadxl.com]tadXL[/url]Thu, 13 Feb 2014 11:03:26 GMTbobthefinancialengineerRE: Calculate XIRR in SQL Serverhttp://www.sqlservercentral.com/Forums/Topic919838-338-1.aspxthanx ...but the xirr value is not marching with MS Excel :(Mon, 31 Jan 2011 23:34:28 GMTraka.nandiRE: Calculate XIRR in SQL Serverhttp://www.sqlservercentral.com/Forums/Topic919838-338-1.aspxok Elutin Thanx four your helpTue, 11 May 2010 22:27:56 GMTvijay.sRE: Calculate XIRR in SQL Serverhttp://www.sqlservercentral.com/Forums/Topic919838-338-1.aspxOK,Here we are: 1. We create a sample table[code="sql"]create table txns( PmtAmount money ,PmtDate Datetime)insert into txns select -2750,'20080205'union select 1000,'20080705'union select 2000,'20090105'[/code]2. Then we calculate your XIRR as per Excel (almost):[code="sql"]DECLARE @nGuessRate NUMERIC(18,10)SET @nGuessRate = 0.1 -- 10% - as per Excel XIRR defaultDECLARE @dtFirstPmtDate DATETIMEDECLARE @nRateChange NUMERIC(18,10)DECLARE @nResidual NUMERIC(18,10)DECLARE @nPrevResidual NUMERIC(18,10)DECLARE @nXIRR NUMERIC(18,10)SET @nRateChange = 0.1 -- starting point of the rate changeSELECT @dtFirstPmtDate=MIN(PmtDate) FROM txnsSELECT @nResidual = 10 ,@nPrevResidual = 1 ,@nXIRR = @nGuessRateDECLARE @nIteration SMALLINTSET @nIteration = 0 -- 100 is a maximum number of calc. iterations as per Excel XIRRWHILE @nIteration < 100 AND ABS((@nPrevResidual - @nResidual) / @nPrevResidual) > 1.0/POWER(10,8)BEGIN SET @nPrevResidual = @nResidual SET @nResidual = 0 SELECT @nResidual = @nResidual + PmtAmount/ POWER((1.0+@nGuessRate),(DATEDIFF(d,@dtFirstPmtDate,PmtDate)/365.0)) FROM txns SET @nXIRR = @nGuessRate IF @nResidual >= 0 BEGIN SET @nGuessRate = @nGuessRate + @nRateChange END ELSE BEGIN SET @nRateChange = @nRateChange / 2 SET @nGuessRate = @nGuessRate - @nRateChange END SET @nIteration = @nIteration + 1END-- Calculation resultSELECT @nXIRR AS XIRR[/code]Now, you can wrap it into stored proc or sql function. Remember to check that your payment range contains at least one negative and one positive payment, otherwise you should return some error, check Excel XIRR documentation for other error conditions if any.BUT! It may not be what you want. You most likely want XIRR to be an aggregate function. You cannot implement it in T-SQL, but you can do it in .NET as CLR function. Or, buy the product I have told you about in the previous post.I might write some article about it if I have spare time...Good LuckTue, 11 May 2010 15:18:19 GMTEugene ElutinRE: Calculate XIRR in SQL Serverhttp://www.sqlservercentral.com/Forums/Topic919838-338-1.aspxa quick google search on XIRR turned up this:[url]http://forums.databasejournal.com/showthread.php?t=46094[/url]Tue, 11 May 2010 11:51:27 GMTCory E.RE: Calculate XIRR in SQL Serverhttp://www.sqlservercentral.com/Forums/Topic919838-338-1.aspxcan any one give me best algorithm for the same, so i can go ahead with development.If sample code provided then its save my time.ThankxTue, 11 May 2010 11:48:12 GMTvijay.sRE: Calculate XIRR in SQL Serverhttp://www.sqlservercentral.com/Forums/Topic919838-338-1.aspxThat the good one!There are no such thing in SQL! You can code it yourself if you wish and have time to do so (you will need to find good algorithm first).Or use XLeratorDB product from www.westclintech.com, if you really need it and have some money to spend :-)Tue, 11 May 2010 10:46:38 GMTEugene ElutinCalculate XIRR in SQL Serverhttp://www.sqlservercentral.com/Forums/Topic919838-338-1.aspxHello All,Is there any way to calculate XIRR in SQL server using T-SQL.Kindly suggest.ThankxTue, 11 May 2010 10:26:29 GMTvijay.s