sqlsql-serverexcelirr

IRR Function to return also negative values


    GO
    /****** Object:  UserDefinedFunction [dbo].[fn_IRR]    Script Date: 7/28/2014 11:43:40 AM ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    ALTER  FUNCTION [dbo].[fn_IRR]
    (
        @str varchar(max),
        @precision DECIMAL(30,10)
    )
    RETURNS DECIMAL(30, 10)
    AS 
    BEGIN
        declare  @AdjValue decimal(30,10)
                ,@guess Decimal(30,10)
                ,@guess_new Decimal(30,10)

        Select @AdjValue = 0.1, @guess=0

        DECLARE @t_IDs TABLE (
            id INT IDENTITY(0, 1),
            value DECIMAL(30, 10)
        )
        Declare @NPV DECIMAL(30, 10)
               ,@iter_cnt int

        INSERT INTO @t_IDs 
            select * from dbo.fn_SplitString(@str,',')

        SET @guess = CASE WHEN ISNULL(@guess, 0) <= 0 THEN 0 ELSE @guess END

        SELECT @NPV = SUM(value / POWER(1 + @guess, id)) FROM @t_IDs
        WHILE ((@NPV > 0 or @AdjValue > @precision) and (isnull(@iter_cnt,0) < 8192))
        BEGIN
            SET @guess_new = @guess + @AdjValue
            SELECT @NPV = SUM(value / POWER(1 + @guess_new, id)) FROM @t_IDs
            set @iter_cnt = isnull(@iter_cnt,0) + 1
            if (@NPV > 0)
                select @guess=@guess_new
            else
                select @AdjValue=@AdjValue/10
        END
        RETURN @guess
    END


USE [absmart_v442]
GO
/****** Object:  UserDefinedFunction [dbo].[fn_SplitString]    Script Date: 7/28/2014 11:47:58 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER FUNCTION [dbo].[fn_SplitString](
     @str nvarchar(max)
    ,@sep nvarchar(max)
)
RETURNS TABLE
AS
RETURN
    WITH a AS(
        SELECT CAST(0 AS BIGINT) as idx1,CHARINDEX(@sep,@str) idx2
        UNION ALL
        SELECT idx2+1,CHARINDEX(@sep,@str,idx2+1)
        FROM a
        WHERE idx2>0
    )
    SELECT SUBSTRING(@str,idx1,COALESCE(NULLIF(idx2,0),LEN(@str)+1)-idx1) as StrValue
    FROM a

So I have this code, that calculates the IRR for a set of values. The function does a great work ( I got the code from an other stackoverflow question ). My only problem is I can't find a way to make it show also the negative values, if the IRR > 0 the function gives a good result if the IRR < 0 it returns 0, but I would like for it to return the negative result.

The problem is that I don't quite understand how the IRR value is calculated or why it's printing 0 if the IRR is negative. Also I can't use the debugger from SQL due to some problems ( I've tried it) . Anyone has any idea how to make sure the function always returns the IRR even if it's a negative value.

select [dbo].[fn_IRR]('-45000,15000,20000,25000.00,10000.00,5000',0.00000000001) as IRR should return 0.2299339513




select [dbo].[fn_IRR]('-170000,32000,35000,33000.00,29000.00,36000',0.00000000001) as IRR

should return -0.98 as it returs in Excel


Solution

  • I managed to to find the solution on my own, you can intialise @guess with -0.99