CaRDiaK
CaRDiaK

Reputation: 885

T-SQL - receive date value and adjust day / month retaining year

I am trying to perform a little date manipulation on a value I will be passing in to a stored procedure.

Example Inputs;

31/12/2008 
15/11/2007 
21/05/2005

Expected Output;

31/12/2012
15/11/2012
21/05/2012

Formatted code from the answer provided;

DECLARE @date DATETIME = '31/12/2007'
DECLARE @year INT 
SET @year = DATEPART(YEAR, GETDATE())
SELECT DATEADD(YEAR, @year - DATEPART(YEAR, @date), @date) 

Upvotes: 0

Views: 216

Answers (1)

Kirk Broadhurst
Kirk Broadhurst

Reputation: 28698

You are trying to convert the year component to 2012?

-- get the year part
DATEPART(YEAR, [Date])

-- get the number of years to add
2012 - DATEPART(YEAR, [Date])

-- add that many years to the date
DATEADD(YEAR, 2012 - DATEPART(YEAR, [Date]), [Date])

Upvotes: 4

Related Questions