I have an t-sql script to generate a ‘bill payment’ date based on the a contract bill ‘start date’.
When the date is on e.g. 31st of the month the ‘sql’ works until it reaches e.g. February – where the day becomes 28th and continues along that line.
(Please see & run the sample sql for example)
I understand what’s happening – but im looking for a t-sql guru to come up with a creative way to help generate the ‘correct schedule’. i.e. one that reflects the last day of the month.
Any help would be most welcomed
Kind regards
Dan
--===========================================
--temp table for DateAdd example
--===========================================
DECLARE @dateadd TABLE (
ID INT,
term INT,
BillDate DATE
)
INSERT INTO @dateadd
SELECT 1
, 12
, '2018-12-31 00:00:00.000'
--===========================================
--generate bill table
--===========================================
;WITH bill AS (
SELECT ID=ID
, RepaymentNo=1 --set PaymentNo
, term
, BillDate as BillDate
FROM @dateadd
UNION ALL
SELECT ID
, RepaymentNo + 1 as PaymentNo
, term
, DateAdd(month,1,BillDate)
FROM bill
WHERE RepaymentNo < term)
--=========================================================
--notice how the 'BillDate' becomes the 28th after feb 2019
--=========================================================
select * FROM bill