Find Next Anniversary Date (T-SQL)


http://social.msdn.microsoft.com/Forums/sqlserver/en-US/9dceaf0b-c939-43fa-bf60-b2377a096d4a/anniversary-date-calculation?forum=transactsql#fa6c226a-ef27-4c5e-8d5f-7eaac4e4dec3

 
 create table test1
(InvestorID int ,pension_start_date datetime)
INSERT INTO test1 (InvestorID,pension_start_date) values (1,'01/01/14'), (2,'05/05/13'),(3,'01/31/13'),(4,'10/25/12'),(5,'03/25/10')

select InvestorID, pension_start_date, CASE WHEN
 (DATEADD(year,DATEDIFF(year, pension_start_date  ,current_timestamp) , pension_start_date) > current_timestamp)
THEN Dateadd(Year,DATEDIFF(year, pension_start_date  ,current_timestamp) , pension_start_date)
ELSE  Dateadd(Year,DATEDIFF(year, pension_start_date  ,current_timestamp)+1 , pension_start_date) END [Anniversary Date] 
FROM test1

--Clean up
DROP TABLE test1		

 
Advertisements


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s