Updating year in datetime variable

JGreasley picture JGreasley · May 11, 2012 · Viewed 8.2k times · Source

I'm playing around with manipulating a datetime variable. I can't seem to find a way to update a datetime variable year to the current year.

For example I have

2007-12-01 00:00:00.000

But I would like that to be

2012-12-01 00:00:00.000    (The current year were in)

I've been playing with datediff, but I can't seem to nail it.

Any advice would be appreciated.

Thanks

Answer

AdaTheDev picture AdaTheDev · May 11, 2012
DECLARE @date datetime = '2007-01-09T12:34:56'
SELECT @date = DATEADD(yyyy, DATEDIFF(yyyy, @date, GETDATE()), @date)

SELECT @date