Logo

SQL Server UNEQUALLOANPAYMENTS Function

Updated 2023-10-11 14:48:55.630000

Description

Use the table-valued function UNEQUALLOANPAYMENTS to generate a payment schedule for a loan where the interest payment frequency and the principal payment frequency are different, or the loan starts with an interest only schedule with principal repayments commencing after the first interest payment date.

Syntax

SELECT * FROM [westclintech].[wct].[UNEQUALLOANPAYMENTS](
  <@PV, float,>
 ,<@Rate, float,>
 ,<@LoanDate, datetime,>
 ,<@InterestFrequency, int,>
 ,<@FirstPaymentDate, datetime,>
 ,<@DaysInYr, int,>
 ,<@PrinPaymentMultiple, int,>
 ,<@FirstPrinPayNo, int,>
 ,<@NumberOfPayments, int,>
 ,<@LastPaymentNumber, int,>
 ,<@FV, float,>
 ,<@IsRegPay, bit,>)

Arguments

@PV

the principal amount of the loan or lease. @PV is an expression of type float or of a type that can be implicitly converted to float.

@Rate

the annual interest rate for the loan. @Rate is an expression of type float or of a type that can be implicitly converted to float.

@LoanDate

the date that the loan starts accruing interest. @LoanDate is an expression of type datetime or of a type that can be implicitly converted to datetime.

@InterestFrequency

the number if times that interest is paid in a year. @InterestFrequency is an expression of type int or of a type that can be implicitly converted to int.

@FirstPaymentDate

the date that the first payment is due. @FirstPaymentDate is an expression of type datetime or of a type that can be implicitly converted to datetime.

@DaysInYr

the denominator number of days to be used in the calculation of the interest amount in the odd first period. @DaysInYr is an expression of type int or of a type that can be implicitly converted to int.

@PrinPaymentMultiple

the ratio of the frequency of the interest payments to the frequency of the interest payments. For example, a loan with monthly payments of interest and quarterly payments of principal would have a @PrinPaymentMultiple of 3. @PrinPaymentMultiple is an expression of type int or of a type that can be implicitly converted to int.

@FirstPrinPayNo

the payment number of the first principal payment. @FirstPrinPayNo is an expression of type int or of a type that can be implicitly converted to int.

@NumberOfPayments

the total number of payments to be used in the calculation of the periodic payments. This may not be the actual number of payments on the loan, which can be specified by using @LastPaymentNumber. @NumberOfPayments is an expression of type int or of a type that can be implicitly converted to int.

@LastPaymentNumber

the number of the last loan payment if different than the @NumberOfPayments. @LastPaymentNumber is an expression of type int or of a type that can be implicitly converted to int.

@FV

the future value at the end of the loan. @FV is an expression of type float or of a type that can be implicitly converted to float.

Return Type

table

colNamecolDatatypecolDesc
num_pmtintThe payment number calcaulated chronologically from @FirstPaymentDate.
date_pmtdatetimeThe date of the payment.
amt_prin_initfloatThe principal amount at the beginning of the period. When num_pmt is equal to 0, the principal amount is the amount of the loan, otherwise the principal amount is the ending principal amount where num_pmt = num_pmt - 1.
amt_pmtfloatThe calculated payment amount
amt_int_payfloatThe interest portion of amt_pmt. In num_pmt > 1 or num_pmt = 1 and @IsRegularPay = 'TRUE', amt_int_pay = @Rate / @InterestFrequency * amt_prin_init. If num_pmt = 1 and @IsRegularPay = 'FALSE' then amt_int_pay is calculated using the number of days from @IssueDate to @FirstPaymentDate based on @DaysInYr
amt_prin_payfloatThe principal portion on amt_pmt calculated as amt_pmt – amt_int_pay
amt_prin_endfloatThe ending principal amount. Calculated as the beginning principal amount (amt_prin_init) less the principal payment amount for the period (amt_prin_pay).

Remarks

If @PV is NULL then @PV = 0.

If @Rate is NULL then @Rate = 0.

If @LoanDate is NULL then @LoanDate = GETDATE().

If @InterestFrequency is NULL then @InterestFrequency = 12.

If @DaysInYr is NULL then @DaysInYr = 365.

If @NumberOfPayments is NULL then @NumberOfPayments = 1.

If @LastPaymentNumber is NULL then @LastPaymentNumber = @NumberOfPayments.

If @FV is NULL then @FV = 0.

If @IsRegularPay is NULL then @IsRegularPay = 'TRUE'.

If @FirstPaymentDate is NULL then @FirstPaymentDate is calculated using @LoanDate and @InterestFrequency.

@InterestFrequency must be 1, 2, 3, 4, 6, 12, 13, 24, 26, 52 or 365.

@NumberOfPayments must be greater than 1.

@Rate must be greater than zero.

@DaysInYr must be 360 or 365.

If @NumberOfPayments is less than 1 then an error will be generated.

If @PrinPaymentMultiple is less than 1 then an error will be generated.

If @LastPaymentNumber is less than 1 then an error will be generated.

If @FirstPrinPayNo < 2 then an error will be generated.

Examples

A 1,000,000 loan dated 2014-01-15 with monthly payments commencing on 2014-02-15 and quarterly principal payments commencing with the third interest payment. Notice that the quarterly payments are constant at 130,690.30 but that interest payments very from quarter to quarter though they are the same for each month between principal payments.

SELECT *
FROM wct.UnequalLoanPayments(   1000000,      --@PV
                                0.12,         --@Rate
                                '2014-01-15', --@LoanDate
                                12,           --@IntPmtPerYr
                                '2014-02-15', --@FirstPaymentDate
                                NULL,         --@DaysInYr
                                3,            --@PrinPayMultiple
                                3,            --@FirstPrinPayNo
                                24,           --@NumberOfPayments
                                NULL,         --@LastPaymentNumber
                                0,            --@FV
                                NULL          --@IsRegPay
                            ) k;

This produces the following result.

num_pmtdate_pmtamt_prin_initamt_pmtamt_int_payamt_prin_payamt_prin_end
02014-01-15 00:00:00.00000001000000
12014-02-15 00:00:00.0001000000100001000001000000
22014-03-15 00:00:00.0001000000100001000001000000
32014-04-15 00:00:00.0001000000130690.29204331510000120690.292043315879309.707956685
42014-05-15 00:00:00.000879309.7079566858793.097079566858793.097079566850879309.707956685
52014-06-15 00:00:00.000879309.7079566858793.097079566858793.097079566850879309.707956685
62014-07-15 00:00:00.000879309.707956685130690.2920433158793.09707956685121897.194963748757412.512992936
72014-08-15 00:00:00.000757412.5129929367574.125129929367574.125129929360757412.512992936
82014-09-15 00:00:00.000757412.5129929367574.125129929367574.125129929360757412.512992936
92014-10-15 00:00:00.000757412.512992936130690.2920433157574.12512992936123116.166913386634296.346079551
102014-11-15 00:00:00.000634296.3460795516342.963460795516342.963460795510634296.346079551
112014-12-15 00:00:00.000634296.3460795516342.963460795516342.963460795510634296.346079551
122015-01-15 00:00:00.000634296.346079551130690.2920433156342.96346079551124347.32858252509949.017497031
132015-02-15 00:00:00.000509949.0174970315099.490174970315099.490174970310509949.017497031
142015-03-15 00:00:00.000509949.0174970315099.490174970315099.490174970310509949.017497031
152015-04-15 00:00:00.000509949.017497031130690.2920433155099.49017497031125590.801868345384358.215628686
162015-05-15 00:00:00.000384358.2156286863843.582156286863843.582156286860384358.215628686
172015-06-15 00:00:00.000384358.2156286863843.582156286863843.582156286860384358.215628686
182015-07-15 00:00:00.000384358.215628686130690.2920433153843.58215628686126846.709887028257511.505741658
192015-08-15 00:00:00.000257511.5057416582575.115057416582575.115057416580257511.505741658
202015-09-15 00:00:00.000257511.5057416582575.115057416582575.115057416580257511.505741658
212015-10-15 00:00:00.000257511.505741658130690.2920433152575.11505741658128115.176985899129396.328755759
222015-11-15 00:00:00.000129396.3287557591293.963287557591293.963287557590129396.328755759
232015-12-15 00:00:00.000129396.3287557591293.963287557591293.963287557590129396.328755759
242016-01-15 00:00:00.000129396.328755759130690.2920433171293.96328755759129396.3287557590

This loan, has monthly payments and is interest only for the first 6 months, with payments calculated assuming a 25-year maturity but payoff is at the end of 5 years.

SELECT *
FROM wct.UnequalLoanPayments(   1000000,      --@PV
                                0.05,         --@Rate
                                '2014-01-15', --@LoanDate
                                12,           --@IntPmtPerYr
                                '2014-02-15', --@FirstPaymentDate
                                NULL,         --@DaysInYr
                                1,            --@PrinPayMultiple
                                7,            --@FirstPrinPayNo
                                300,          --@NumberOfPayments
                                60,           --@LastPaymentNumber
                                0,            --@FV
                                NULL          --@IsRegPay
                            ) k;

This produces the following result.

num_pmtdate_pmtamt_prin_initamt_pmtamt_int_payamt_prin_payamt_prin_end
02014-01-15 00:00:00.00000001000000
12014-02-15 00:00:00.00010000004166.666666666674166.6666666666701000000
22014-03-15 00:00:00.00010000004166.666666666674166.6666666666701000000
32014-04-15 00:00:00.00010000004166.666666666674166.6666666666701000000
42014-05-15 00:00:00.00010000004166.666666666674166.6666666666701000000
52014-06-15 00:00:00.00010000004166.666666666674166.6666666666701000000
62014-07-15 00:00:00.00010000004166.666666666674166.6666666666701000000
72014-08-15 00:00:00.00010000005906.02934245424166.666666666671739.36267578754998260.637324212
82014-09-15 00:00:00.000998260.6373242125906.02934245424159.419322184221746.61002026999996514.027303942
92014-10-15 00:00:00.000996514.0273039425906.02934245424152.141780433091753.88756202111994760.139741921
102014-11-15 00:00:00.000994760.1397419215906.02934245424144.833915591341761.19542686287992998.944315058
112014-12-15 00:00:00.000992998.9443150585906.02934245424137.495601312741768.53374114146991230.410573917
122015-01-15 00:00:00.000991230.4105739175906.02934245424130.126710724651775.90263172955989454.507942187
132015-02-15 00:00:00.000989454.5079421875906.02934245424122.727116425781783.30222602842987671.205716159
142015-03-15 00:00:00.000987671.2057161595906.02934245424115.2966904841790.73265197021985880.473064189
152015-04-15 00:00:00.000985880.4730641895906.02934245424107.835304434121798.19403802008984082.279026169
162015-05-15 00:00:00.000984082.2790261695906.02934245424100.34282927571805.6865131785982276.59251299
172015-06-15 00:00:00.000982276.592512995906.02934245424092.819135470791813.21020698341980463.382306007
182015-07-15 00:00:00.000980463.3823060075906.02934245424085.264092941691820.76524951251978642.617056494
192015-08-15 00:00:00.000978642.6170564945906.02934245424077.677571068731828.35177138548976814.265285109
202015-09-15 00:00:00.000976814.2652851095906.02934245424070.059438687951835.96990376625974978.295381342
212015-10-15 00:00:00.000974978.2953813425906.02934245424062.409564088931843.61977836528973134.675602977
222015-11-15 00:00:00.000973134.6756029775906.02934245424054.72781501241851.3015274418971283.374075535
232015-12-15 00:00:00.000971283.3740755355906.02934245424047.014058648061859.01528380614969424.358791729
242016-01-15 00:00:00.000969424.3587917295906.02934245424039.26816163221866.761180822967557.597610907
252016-02-15 00:00:00.000967557.5976109075906.02934245424031.489990045451874.53935240876965683.058258498
262016-03-15 00:00:00.000965683.0582584985906.02934245424023.679409410411882.34993304379963800.708325455
272016-04-15 00:00:00.000963800.7083254555906.02934245424015.836284689391890.19305776481961910.51526769
282016-05-15 00:00:00.000961910.515267695906.02934245424007.960480282041898.06886217216960012.446405518
292016-06-15 00:00:00.000960012.4464055185906.02934245424000.051860022991905.97748243121958106.468923086
302016-07-15 00:00:00.000958106.4689230865906.02934245423992.110287179531913.91905527468956192.549867812
312016-08-15 00:00:00.000956192.5498678125906.02934245423984.135624449221921.89371800499954270.656149807
322016-09-15 00:00:00.000954270.6561498075906.02934245423976.127733957531929.90160849668952340.75454131
332016-10-15 00:00:00.000952340.754541315906.02934245423968.086477255461937.94286519875950402.811676111
342016-11-15 00:00:00.000950402.8116761115906.02934245423960.011715317131946.01762713707948456.794048974
352016-12-15 00:00:00.000948456.7940489745906.02934245423951.903308537391954.12603391681946502.668015057
362017-01-15 00:00:00.000946502.6680150575906.02934245423943.761116729411962.2682257248944540.399789333
372017-02-15 00:00:00.000944540.3997893335906.02934245423935.584999122221970.44434333199942569.955446001
382017-03-15 00:00:00.000942569.9554460015906.02934245423927.374814358341978.65452809587940591.300917905
392017-04-15 00:00:00.000940591.3009179055906.02934245423919.130420491271986.89892196293938604.401995942
402017-05-15 00:00:00.000938604.4019959425906.02934245423910.851674983091995.17766747111936609.224328471
412017-06-15 00:00:00.000936609.2243284715906.02934245423902.538434701962003.49090775224934605.733420718
422017-07-15 00:00:00.000934605.7334207185906.02934245423894.190555919662011.83878653454932593.894634184
432017-08-15 00:00:00.000932593.8946341845906.02934245423885.80789430912020.2214481451930573.673186039
442017-09-15 00:00:00.000930573.6731860395906.02934245423877.390304941832028.63903751238928545.034148526
452017-10-15 00:00:00.000928545.0341485265906.02934245423868.937642285532037.09170016868926507.942448358
462017-11-15 00:00:00.000926507.9424483585906.02934245423860.449760201492045.57958225271924462.362866105
472017-12-15 00:00:00.000924462.3628661055906.02934245423851.92651194212054.1028305121922408.260035593
482018-01-15 00:00:00.000922408.2600355935906.02934245423843.36775014832062.6615923059920345.598443287
492018-02-15 00:00:00.000920345.5984432875906.02934245423834.773326847032071.25601560717918274.34242768
502018-03-15 00:00:00.000918274.342427685906.02934245423826.143093448672079.88624900554916194.456178674
512018-04-15 00:00:00.000916194.4561786745906.02934245423817.476900744482088.55244170973914105.903736965
522018-05-15 00:00:00.000914105.9037369655906.02934245423808.774598904022097.25474355018912008.648993415
532018-06-15 00:00:00.000912008.6489934155906.02934245423800.036037472562105.99330498164909902.655688433
542018-07-15 00:00:00.000909902.6556884335906.02934245423791.261065368472114.76827708573907787.887411347
552018-08-15 00:00:00.000907787.8874113475906.02934245423782.449530880612123.57981157359905664.307599774
562018-09-15 00:00:00.000905664.3075997745906.02934245423773.601281665722132.42806078848903531.879538985
572018-10-15 00:00:00.000903531.8795389855906.02934245423764.716164745772141.31317770843901390.566361277
582018-11-15 00:00:00.000901390.5663612775906.02934245423755.794026505322150.23531594888899240.331045328
592018-12-15 00:00:00.000899240.3310453285906.02934245423746.834712688872159.19462976534897081.136415563
602019-01-15 00:00:00.000897081.136415563900818.9744839613737.83806839818897081.1364155630