Setelah menambahkan satu bulan, bandingkan DAY. Jika kurang karena DATEADD pergi ke akhir bulan (misalnya 31 Jan sampai 28 Feb), maka lewati ke bulan berikutnya
DATEADD/DATEDIFF di sini melompat ke awal bulan berikutnya
declare @StartEffectiveDate datetime
set @StartEffectiveDate = '2011-01-20'
SELECT
CASE
WHEN DAY(@StartEffectiveDate) <= DAY(DATEADD (MONTH, 1, @StartEffectiveDate)) THEN DATEADD (MONTH, 1, @StartEffectiveDate)
ELSE DATEADD(day, 1, DATEADD (MONTH, 1, @StartEffectiveDate))
END
set @StartEffectiveDate = '2011-01-31'
SELECT
CASE
WHEN DAY(@StartEffectiveDate) <= DAY(DATEADD (MONTH, 1, @StartEffectiveDate)) THEN DATEADD (MONTH, 1, @StartEffectiveDate)
ELSE DATEADD(day, 1, DATEADD (MONTH, 1, @StartEffectiveDate))
END
set @StartEffectiveDate = '2011-02-28'
SELECT
CASE
WHEN DAY(@StartEffectiveDate) <= DAY(DATEADD (MONTH, 1, @StartEffectiveDate)) THEN DATEADD (MONTH, 1, @StartEffectiveDate)
ELSE DATEADD(day, 1, DATEADD (MONTH, 1, @StartEffectiveDate))
END
Sunting:hanya perlu menambahkan satu hari ekstra daripada DATEADD/DATEDIFF yang mewah...