Can someone help me figure out a formula to be used in SQL Server similar to the Excel formula pasted below?
=EDATE(date,(DATEDIF(date,TODAY(),”y”)+1)*12)
I want to capture the next anniversary date.
ID, Date, Next Anniversary 123, 01/12/20, 01/12/21 234, 05/25/17, 05/25/20
The information I have is ID and date. I want to determine next anniversary date.
Advertisement
Answer
Seems you could achieve this fairly easily with DATEADD
and DATEDIFF
, and a CASE
expression if the generated date is less than the current date:
SELECT YourDate, CASE WHEN V.NextDate < CONVERT(date,GETDATE()) THEN DATEADD(YEAR, 1, V.NextDate) ELSE V.NextDate END AS NextDate FROM (VALUES(CONVERT(date,'20190101')), --This would actually be your table (CONVERT(date,'20170709')), (CONVERT(date,'20160229')), --Leap year (CONVERT(date,'20181219')))YT(YourDate) CROSS APPLY (VALUES(DATEADD(YEAR, DATEDIFF(YEAR, YT.YourDate, GETDATE()),YT.YourDate)))V(NextDate);