# SQL Server straight-line principal amortization

ConstantPrincipal

Updated: 18 September 2014

Use the table-valued function ConstantPrincipal to return the cash flow schedule for a loan with a fixed maturity date where the principal is reduced on a straight-line basis. ConstantPrincipal computes the periodic interest and principal amounts bring the loan balance to zero on the maturity date. ConstantPrincipal supports separate interest and principal repayment schedules.
The interest and principal payment periods are entered in ConstantPrincipal as the number of months between payments. For example, a loan with monthly payments of interest would have an interest frequency of 1. If principal is to be repaid every months, then it would have a principal payment frequency of 6.
ConstantPrincipal supports both an initial grace period and an additional grace period during the life of the loan, for principal and/or interest. All payments and their associated dates are calculated with respect to the reference date supplied to the function (which should not be confused with the start date). If an initial interest grace period is entered in ConstantPrincipal and it is greater than the reference date, then it becomes the first interest payment date and subsequent interest payments are calculated from that date forward. The same principal applies for principal payments.
If any payments would otherwise occur in the specified grace period, then that payment is moved to the end of the grace period and all remaining payments are calculated from the end of the grace period.
If no initial grace period is specified then the first payment date is calculated using the respective payment frequency. If the start date has been entered and the number of months between the start date and the reference date is less than the frequency, then the first payment date is calculated by adding the respective frequency (as a number of months) to the start date.
If no start date has been entered but a previous payment date has been entered and the number of months between the previous payment date and the reference date is less than the frequency, then the first payment date is calculated by adding the respective frequency (as a number of months) to the previous payment date.
If there is no start date and previous payment dates or the number of months between those dates and the reference date is greater than the frequency, then the first payment date is calculated by adding the resepctive frequency (as a number of months) to the reference date.
All payments in the resultant table are moved to the end of the month and interest is calculated using these end-of-month dates.
The interest rate is calculated as:

Where:

 I = InterestRate R = @InterestRate F = @Frequency T = Time, in years, from the previous interest payment date to PaymentDate

In the case where there are one or more principal payments between interest payment dates, the interest payment amount is calculated using the outstanding principal balances during the interest payment period.
If the irregular period is longer than the regular period then the interest amount is broken out into the regular interest amount and a 'grace' interest amount.
Syntax
SELECT * FROM [wct].[ConstantPrincipal](
<@OutstandingAmount, float,>
,<@InterestBasis, nvarchar(4000),>
,<@InterestRate, float,>
,<@PrincipalPaymentFrequency, int,>
,<@InterestPaymentFrequency, int,>
,<@LastPrinPayAmount, float,>
,<@MaturityDate, datetime,>
,<@ReferenceDate, datetime,>
,<@PrevPrincipalPayDate, datetime,>
,<@PrevInterestPayDate, datetime,>
,<@StartDate, datetime,>
,<@FirstPrincipalPayDate, datetime,>
,<@FirstInterestPayDate, datetime,>
,<@PrincipalGracePeriodStartDate, datetime,>
,<@PrincipalGracePeriodEndDate, datetime,>
,<@InterestGracePeriodStartDate, datetime,>
,<@InterestGracePeriodEndDate, datetime,>)
Arguments
@OutstandingAmount
the principal amount of the loan. @OutstandingAmount is an expression of type float or of a type that can be implicitly converted to float.
@InterestBasis
the day count convention used to calculate the interest amount. @InterestBasis can be 30/360, Actual/360, Actual/365, or Actual/Actual. @InterestBasis is an expression of the character string data type category.
@InterestRate
the annual rate of interest for the loan. @InterestRate is an expression of type float or of a type that can be implicitly converted to float
@PrinicpalPaymentFrequency
the number of months between principal payments. @PrincipalPaymentFrequency is an expression of type int or of a type that can be implicitly converted to int.
@InterestPaymentFrequency
the number of months in a regular interest payment. @InterestPaymentFrequency is an expression of type int or of a type that can be implicitly converted to int.
@LastPrinPayAmount
the amount of the principal payment due on the maturity date. @LastPrinPayAmount is an expression of type float or of a type that can be implicitly converted to float.
@MaturityDate
the maturity date of the loan. @MaturityDate is an expression that returns a datetime or smalldatetime value, or a character string in date format.
@ReferenceDate
the starting date for the number of months with respect to all other dates. @ReferenceDate is an expression that returns a datetime or smalldatetime value, or a character string in date format.
@PrevPrincipalPayDate
the last principal payment date prior to the reference date. @PrevPrincipalPayDate is an expression that returns a datetime or smalldatetime value, or a character string in date format.
@PrevInterestPayDate
the last interest payment date prior to the reference date. @PrevPrincipalPayDate is an expression that returns a datetime or smalldatetime value, or a character string in date format.
@StartDate
the start date of the loan. @StartDate is an expression that returns a datetime or smalldatetime value, or a character string in date format.
@FirstPrincipalPayDate
the first principal payment date of the loan if other than a regular periodic payment. @FirstPayDate is an expression that returns a datetime or smalldatetime value, or a character string in date format.
@FirstInterestPayDate
the first interest payment date of the loan if other than a regular periodic payment. @FirstPayDate is an expression that returns a datetime or smalldatetime value, or a character string in date format.
@PrincipalGracePeriodStartDate
the date on which the (interim) principal grace period commences. @PrincipalGracePeriodStartDate is an expression that returns a datetime or smalldatetime value, or a character string in date format.
@PrincipalGracePeriodEndDate
the date on which the (interim) principal grace period concludes. @PrincipalGracePeriodEndDate is an expression that returns a datetime or smalldatetime value, or a character string in date format.

@InterestGracePeriodStartDate
the date on which the (interim) interest grace period commences. @InterestGracePeriodStartDate is an expression that returns a datetime or smalldatetime value, or a character string in date format.
@PrincipalGracePeriodEndDate
the date on which the (interim) interest grace period concludes. @GracePeriodEndDate is an expression that returns a datetime or smalldatetime value, or a character string in date format.
Return Type
RETURNS TABLE (
[Period] [int] NULL,
[PrincipalPayment] [float] NULL,
[InterestPayment] [float] NULL,
[CashFlow] [float] NULL,
[OutstandingExposure] [float] NULL,
[CapitalAmountInDebt] [float] NULL,
[TotalExposure] [float] NULL,
[NumberOfMonth] [int] NULL,
[PaymentDate] [datetime] NULL,
[GraceInterest] [float] NULL,
[InterestRate] [float] NULL
)

 Column Description Period A reference number uniquely identifying a row in the resultant table. PrincipalPayment The amount of the principal payment. InterestPayment The amount of the regular interest payment. CashFlow PrincipalPayment + InterestPayment + GraceInterest. OutstandingExposure When Period = 0 then @OutstandingAmount. For Period > 0 then OutstandingExposure(Period-1) + InterestPayment. CapitalAmountInDebt When Period = 0, @OutstandingAmount. For Period > 0 then CapitalAmountInDebt(Period-1) – PrincipalPayment TotalExposure When Period = 0, @OutstandingAmount. For Period > 0 then CapitalAmountInDebt(Period-1) + InterestPayment NumberOfMonth The number of months between the @ReferenceDate and the PaymentDate. PaymentDate The end-of-month date of the payment. GraceInterest The amount of the grace interest InterestRate The interest rate from the previous interest payment date to the PaymentDate.

Remarks
·         The PaymentDate for all rows is generated as the last day of the month.
·         For Period = 0, PrincipalPayment, InterestPayment, CashFlow, NumberOfMonth, GraceInterest, and InterestRate are set to 0.
·         If @Frequency is NULL then @Frequency = 1.
·         If @InterestRate is NULL then @InterestRate = 0.
·         If @ReferenceDate is NULL then @ReferenceDate = GETDATE().
·         GraceInterest is only calculated on @FirstInterestPayDate and @InterestGracePeriodEndDate.
·         GraceInterest is only calculated if length of the grace interst periods is greater than @PaymentFrequency.
·         GraceInterest is the difference between the interest for the period from the previous interest payment to PaymentDate and interest that would have been calculated for a period with length equal to @PaymentFrequency.
·         CashFlow may not equal PaymentAmount on @FirstPayDate or @GracePeriodEndDate due to GraceInterest.
·         The last row returned will always be for the maturity date and may be shorter than a regular period depending on the combination of dates and @PaymentFrequency
Examples
This is a simple 5-year 100,000 loan with quarterly payments of principal and interest and an interest rate of 4.5%.
SELECT
*
FROM wct.ConstantPrincipal(
100000            --@OutstandingAmount
,'30/360'          --@InterestBasis
,.045              --@InterestRate
,3                 --@FreqPayPrincipal
,3                 --@FreqPayInterest
,10000.00          --@LastPrinPayAmount
,'2019-10-01'      --@MaturityDate
,'2014-10-01'      --@ReferenceDate
,NULL              --@PrevPrincipalPayDate
,NULL              --@PrevInterestPayDate
,NULL              --@StartDate
,NULL              --@FirstPrincipalPayDate
,NULL              --@FirstInterestPayDate
,NULL              --@PrincipalGracePeriodStartDate
,NULL              --@PrincipalGracePeriodEndDate
,NULL              --@InterestGracePeriodStartDate
,NULL              --@InterestGracePeriodEndDate
)
This produces the following result (which has been reformatted for ease of viewing).

 Period Principal Payment Interest Payment Cash Flow Outstanding Exposure Capital Amount In Debt Total Exposure Number Of Month Payment Date Grace Interest Interest Rate 0 0.00 0.00 0.00 100000.00 100000.00 100000.00 0 2014-10-31 0.00 0.000000 1 4736.84 1125.00 5861.84 101125.00 95263.16 101125.00 3 2015-01-31 0.00 0.011250 2 4736.84 1071.71 5808.55 102196.71 90526.32 96334.87 6 2015-04-30 0.00 0.011250 3 4736.84 1018.42 5755.26 103215.13 85789.47 91544.74 9 2015-07-31 0.00 0.011250 4 4736.84 965.13 5701.97 104180.26 81052.63 86754.61 12 2015-10-31 0.00 0.011250 5 4736.84 911.84 5648.68 105092.11 76315.79 81964.47 15 2016-01-31 0.00 0.011250 6 4736.84 858.55 5595.39 105950.66 71578.95 77174.34 18 2016-04-30 0.00 0.011250 7 4736.84 805.26 5542.11 106755.92 66842.11 72384.21 21 2016-07-31 0.00 0.011250 8 4736.84 751.97 5488.82 107507.89 62105.26 67594.08 24 2016-10-31 0.00 0.011250 9 4736.84 698.68 5435.53 108206.58 57368.42 62803.95 27 2017-01-31 0.00 0.011250 10 4736.84 645.39 5382.24 108851.97 52631.58 58013.82 30 2017-04-30 0.00 0.011250 11 4736.84 592.11 5328.95 109444.08 47894.74 53223.68 33 2017-07-31 0.00 0.011250 12 4736.84 538.82 5275.66 109982.89 43157.89 48433.55 36 2017-10-31 0.00 0.011250 13 4736.84 485.53 5222.37 110468.42 38421.05 43643.42 39 2018-01-31 0.00 0.011250 14 4736.84 432.24 5169.08 110900.66 33684.21 38853.29 42 2018-04-30 0.00 0.011250 15 4736.84 378.95 5115.79 111279.61 28947.37 34063.16 45 2018-07-31 0.00 0.011250 16 4736.84 325.66 5062.50 111605.26 24210.53 29273.03 48 2018-10-31 0.00 0.011250 17 4736.84 272.37 5009.21 111877.63 19473.68 24482.89 51 2019-01-31 0.00 0.011250 18 4736.84 219.08 4955.92 112096.71 14736.84 19692.76 54 2019-04-30 0.00 0.011250 19 4736.84 165.79 4902.63 112262.50 10000.00 14902.63 57 2019-07-31 0.00 0.011250 20 10000.00 112.50 10112.50 112375.00 0.00 10112.50 60 2019-10-31 0.00 0.011250

We modify the SQL to have monthly payments of principal and quarterly payments of interest.
SELECT
*
FROM wct.ConstantPrincipal(
100000            --@OutstandingAmount
,'30/360'          --@InterestBasis
,.045              --@InterestRate
,1                 --@FreqPayPrincipal
,3                 --@FreqPayInterest
,10000.00          --@LastPrinPayAmount
,'2019-10-01'      --@MaturityDate
,'2014-10-01'      --@ReferenceDate
,NULL              --@PrevPrincipalPayDate
,NULL              --@PrevInterestPayDate
,NULL              --@StartDate
,NULL              --@FirstPrincipalPayDate
,NULL              --@FirstInterestPayDate
,NULL              --@PrincipalGracePeriodStartDate
,NULL              --@PrincipalGracePeriodEndDate
,NULL              --@InterestGracePeriodStartDate
,NULL              --@InterestGracePeriodEndDate
)
This produces the following result.

 Period Principal Payment Interest Payment Cash Flow Outstanding Exposure Capital Amount In Debt Total Exposure Number Of Month Payment Date Grace Interest Interest Rate 0 0.00 0.00 0.00 100000.00 100000.00 100000.00 0 2014-10-31 0.00 0.000000 1 1525.42 0.00 1525.42 100000.00 98474.58 100000.00 1 2014-11-30 0.00 0.003736 2 1525.42 0.00 1525.42 100000.00 96949.15 98474.58 2 2014-12-31 0.00 0.007486 3 1525.42 1107.88 2633.31 101107.88 95423.73 98057.03 3 2015-01-31 0.00 0.011250 4 1525.42 0.00 1525.42 101107.88 93898.31 95423.73 4 2015-02-28 0.00 0.003736 5 1525.42 0.00 1525.42 101107.88 92372.88 93898.31 5 2015-03-31 0.00 0.007486 6 1525.42 1056.40 2581.82 102164.28 90847.46 93429.28 6 2015-04-30 0.00 0.011250 7 1525.42 0.00 1525.42 102164.28 89322.03 90847.46 7 2015-05-31 0.00 0.003736 8 1525.42 0.00 1525.42 102164.28 87796.61 89322.03 8 2015-06-30 0.00 0.007486 9 1525.42 1004.92 2530.34 103169.20 86271.19 88801.53 9 2015-07-31 0.00 0.011250 10 1525.42 0.00 1525.42 103169.20 84745.76 86271.19 10 2015-08-31 0.00 0.003736 11 1525.42 0.00 1525.42 103169.20 83220.34 84745.76 11 2015-09-30 0.00 0.007486 12 1525.42 953.43 2478.86 104122.63 81694.92 84173.77 12 2015-10-31 0.00 0.011250 13 1525.42 0.00 1525.42 104122.63 80169.49 81694.92 13 2015-11-30 0.00 0.003736 14 1525.42 0.00 1525.42 104122.63 78644.07 80169.49 14 2015-12-31 0.00 0.007486 15 1525.42 901.95 2427.37 105024.58 77118.64 79546.02 15 2016-01-31 0.00 0.011250 16 1525.42 0.00 1525.42 105024.58 75593.22 77118.64 16 2016-02-29 0.00 0.003736 17 1525.42 0.00 1525.42 105024.58 74067.80 75593.22 17 2016-03-31 0.00 0.007486 18 1525.42 850.47 2375.89 105875.04 72542.37 74918.26 18 2016-04-30 0.00 0.011250 19 1525.42 0.00 1525.42 105875.04 71016.95 72542.37 19 2016-05-31 0.00 0.003736 20 1525.42 0.00 1525.42 105875.04 69491.53 71016.95 20 2016-06-30 0.00 0.007486 21 1525.42 798.98 2324.41 106674.03 67966.10 70290.51 21 2016-07-31 0.00 0.011250 22 1525.42 0.00 1525.42 106674.03 66440.68 67966.10 22 2016-08-31 0.00 0.003736 23 1525.42 0.00 1525.42 106674.03 64915.25 66440.68 23 2016-09-30 0.00 0.007486 24 1525.42 747.50 2272.92 107421.53 63389.83 65662.75 24 2016-10-31 0.00 0.011250 25 1525.42 0.00 1525.42 107421.53 61864.41 63389.83 25 2016-11-30 0.00 0.003736 26 1525.42 0.00 1525.42 107421.53 60338.98 61864.41 26 2016-12-31 0.00 0.007486 27 1525.42 696.02 2221.44 108117.54 58813.56 61035.00 27 2017-01-31 0.00 0.011250 28 1525.42 0.00 1525.42 108117.54 57288.14 58813.56 28 2017-02-28 0.00 0.003736 29 1525.42 0.00 1525.42 108117.54 55762.71 57288.14 29 2017-03-31 0.00 0.007486 30 1525.42 644.53 2169.96 108762.08 54237.29 56407.25 30 2017-04-30 0.00 0.011250 31 1525.42 0.00 1525.42 108762.08 52711.86 54237.29 31 2017-05-31 0.00 0.003736 32 1525.42 0.00 1525.42 108762.08 51186.44 52711.86 32 2017-06-30 0.00 0.007486 33 1525.42 593.05 2118.47 109355.13 49661.02 51779.49 33 2017-07-31 0.00 0.011250 34 1525.42 0.00 1525.42 109355.13 48135.59 49661.02 34 2017-08-31 0.00 0.003736 35 1525.42 0.00 1525.42 109355.13 46610.17 48135.59 35 2017-09-30 0.00 0.007486 36 1525.42 541.57 2066.99 109896.70 45084.75 47151.74 36 2017-10-31 0.00 0.011250 37 1525.42 0.00 1525.42 109896.70 43559.32 45084.75 37 2017-11-30 0.00 0.003736 38 1525.42 0.00 1525.42 109896.70 42033.90 43559.32 38