Input :
Month, Year, GraceMonth. All are in Integer.
What we need to do ?
First need to construct the date from Month, Year and Day(need to get from the current date)
and then add GraceMonth to it.We will get the new Date obviously after adding the GraceMonth.
Next, from the date constructed, we need to compare that with the current date.
What have you tried?
e.g. ( I am showing 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) --construct initial date
,[Add Grace period] =DATEADD(mm,@Graceperiod,DATEADD(mm, (@YEAR - 1900) * 12 + @Month - 1 , DAY(GETDATE()) - 1)) --add grace month
,[DateDiff] = DATEDIFF
(
DAY,
DATEADD(mm, (@YEAR - 1900) * 12 + (@Month + @Graceperiod) - 1 , DAY(GETDATE()) - 1),
GETDATE()
) -- datediff
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 apart from this? The more concise without Casting, the better. And please provide explanation if it involves some tricky part (e.g. some tricky mathematical calculation).
Thanks in advance.
1 Answer