Atlanta Custom Software Development 

   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools


Have you ever wondered how to use same functionality of IRR function of Excel in T-SQL. Well unfortunately there is no inbuilt function in T-SQL but we can create a user defined function to implement the same logic as IRR of Excel/VB.

Click here to copy the following block
create table IncomeTable (
 amt float,
 d  datetime
)

insert into IncomeTable values (-1000,'19980101')
insert into IncomeTable values (200,'19990101')
insert into IncomeTable values (200,'20000101')
insert into IncomeTable values (200,'20010101')
insert into IncomeTable values (200,'20020101')
insert into IncomeTable values (200,'20030101')
insert into IncomeTable values (200,'20040101')


go


create function irr(
 @d  datetime
) returns decimal(18,10) as begin
 declare @irrPrev float set @irrPrev = 0
 declare @irr float set @irr = 0.1
 declare @pvPrev float
 declare @pv float
 set @pvPrev = (
  select sum(amt)
  from IncomeTable
 )
 set @pv = (
  select sum(amt/power(1e0+@irr,cast(d-@d as float)/360e0))
  from IncomeTable
 )
 while abs(@pv) >= 0.0001 begin
  declare @t float
  set @t = @irrPrev
  set @irrPrev = @irr
  set @irr = @irr + (@t-@irr)*@pv/(@pv-@pvPrev)
  set @pvPrev = @pv
  set @pv = (
   select sum(amt/power(1e0+@irr,cast(d-@d as float)/365e0))
   from IncomeTable
  )
 end
 return @irr
end
go

select dbo.irr('19980101')

go

drop function irr
drop table IncomeTable
go

Another version of IRR can be found at

http://www.blackteaconsulting.com/journal/?p=79


Audit, Notify, Deploy and Manage SSIS
Download the Free 30-day Trial Version...Learn More About This Product...

Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )

Recommanded Links

 


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  Blogs

© 2008 BinaryWorld LLC. All rights reserved.