Michael
Michael

Reputation: 2657

Specific Date but increase Year based on GETDATE()

I'm using SQL Server.

I have a specific date (in dd/mm/yyyy format) i.e. 06/04/2020

However, in a T-SQL View, it needs to be always 1 year from now i.e. if I run today it would return 06/04/2021. And if it executed in 2021 it would return 06/04/2022 - how would I do this?

So I can run the below:

SELECT CONVERT(DATE, DATEADD(year, 1, '06/04/2020'), 103) as MyDate;

Which will give me:

2021-06-04

However, how do I make it self-maintaining?

Upvotes: 0

Views: 51

Answers (1)

Nick
Nick

Reputation: 147196

You can make it generic by extracting the year from GETDATE(), adding 1 and concatenating that to 06/04 (or the date as required), and then converting. For example:

SELECT CONVERT(DATE, CONCAT('06/04/', DATEPART(YEAR, GETDATE()) + 1), 103)

Output:

2021-04-06

Upvotes: 1

Related Questions