I want to create a semi - monthly amortization schedule by using a function.
Here is my function:
CASE WHEN day(@dt) < 16
THEN
CASE WHEN month(@dt) = 2 -- IF FEBRUARY, THEN GET LAST DAY OF MONTH
THEN
eomonth(@dt)
ELSE -- SET AS 30
dateadd(day, 30-day(@dt), @dt) end
ELSE
dateadd(month, 1, dateadd(day, 15-day(@dt), @dt)) -- SET AS 15
END
Let's say I already have a table of schedule like this
instno | amount | duedate
------------------------------
1 | 1000.00 | 1-30-2020
2 | 1000.00 | NULL
3 | 1000.00 | NULL
4 | 1000.00 | NULL
5 | 1000.00 | NULL
I want to fill the remaining 4 installments using the function I created or similar.
So far, I have tried doing,
select *,
dbo.duedategenerator(LAG(duedate,1,'1-30-2020') OVER (ORDER BY b.ilno))
from table
Output should be like this:
instno | amount | duedate
------------------------------
1 | 1000.00 | 1-30-2020
2 | 1000.00 | 2-15-2020
3 | 1000.00 | 2-29-2020
4 | 1000.00 | 3-15-2020
5 | 1000.00 | 3-30-2020