0

我正在尝试对将传递给存储过程的值执行一些日期操作。

示例输入;

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

预期产出;

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

提供的答案中的格式化代码;

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

1 回答 1

4

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])
于 2012-08-14T10:44:18.760 回答