Tuesday 4 September 2018

Loan Repayment Calculator T-SQL Query

--Hey Guys, 
--Hope everyone is doing good. 
--After very long time, I am visiting my own blog to post "loan calculator" query. Recently i have taken personal loan of X amount from bank with an interest rate of 13.99%, which i need to repay with in 3 years. I have surprised to see bank has debited 900 dollars in first month repay (In fact Bank has debited money after 45 days of loan deposit). So, I decided to write a T-SQL query which would show amount of interest, remaining amount of principal amount.  
--Here is the query, Please note that we can also use CURSORS instead of WHILE.  
--Please update respective values for following variables " @MonthlyPay,@PersonalLoanDeposit, @MonthlyBankServiceFee ,@InterestPercentage" before executing below query in order to know how much interest you end up paying for your loan. 
DECLARE @MonthlyPay FLOAT = 1759 -- provide monthly pay

DECLARE @PersonalLoanDeposit FLOAT = 50000 -- provide loan amount
DECLARE @MonthlyBankServiceFee FLOAT = 10 -- provide monthly bank service fee
DECLARE @InterestPercentage FLOAT = 0.1399 -- interest %
DECLARE @TotalInterest FLOAT = 0
DECLARE @MonthNumber INT =0
WHILE ( @PersonalLoanDeposit > 0 )
BEGIN 
DECLARE @MonthlyInterest FLOAT 
SELECT @MonthlyInterest = ( @PersonalLoanDeposit * @InterestPercentage ) /12 
SELECT @PersonalLoanDeposit = ( @PersonalLoanDeposit - @MonthlyPay ) +@MonthlyInterest+@MonthlyBankServiceFee
SELECT @TotalInterest = @TotalInterest + @MonthlyInterest+ @MonthlyBankServiceFee 
SELECT @MonthlyInterest = @MonthlyInterest + @MonthlyBankServiceFee 
SELECT @MonthNumber = @MonthNumber + 1 
SELECT @MonthNumber         MonthNo,@PersonalLoanDeposit PrincipalAmount,@TotalInterest as TotalCumulativeInterest
END 
--Thank you, 
--Narasimha