Calculate loan payment in Transact Sql
Ever need a very rough and simple estimated loan payment in Transact Sql?
declare @terms int
declare @rate float
declare @price float
set @terms = 40
set @rate = 17.95
set @price = 13950
declare @realrate float
declare @pmt float
set @realrate = (@rate/100) / 12
--This does the calculation
set @pmt = @price*(@realrate*(power((1+@realrate),@terms))) / (power((1+@realrate),@terms)-1)
print @pmt
There are lots of resources out there for various formulas for calculating various types of payments. Pick your flavor. (See wikipedia
or
One other caveat: I do not condone the practice of putting business logic like this in the database. This was developed for a legacy system that had no other choice. It is posted here because the syntax is more or less, close to the same regardless of the language used, whether tsql or c#, or anything else.
declare @terms int
declare @rate float
declare @price float
set @terms = 40
set @rate = 17.95
set @price = 13950
declare @realrate float
declare @pmt float
set @realrate = (@rate/100) / 12
--This does the calculation
set @pmt = @price*(@realrate*(power((1+@realrate),@terms))) / (power((1+@realrate),@terms)-1)
print @pmt
Comments
Post a Comment