Entrance:
Month, Year, GraceMonth. All are in Integer.
What do we need to do?
First you need to create a date from the month, year and day (you need to get it from the current date) and then add GraceMonth to it. We will get a new Date, obviously after adding GraceMonth. Next, with the constructed date, we need to compare this with the current date.
What have you tried?
eg. (I show in parts)
DECLARE @Month INT = 11 DECLARE @YEAR INT = 2012 DECLARE @Graceperiod INT = 2 SELECT [Construct Initial Date] = DATEADD(mm, (@YEAR - 1900) * 12 + @Month - 1 , DAY(GETDATE()) - 1)
Result
Construct Initial Date Add Grace period DateDiff 2012-11-14 00:00:00.000 2013-01-14 00:00:00.000 -122
If your answer is correct, then what are you looking for?
Is there any other good approach besides this? The more succinctly without Casting, the better. And please provide an explanation if it involves some complex part (for example, some kind of complicated mathematical calculation).
Thanks in advance.
source share