我正在尝试获取一年中的特定日期。
这是我到目前为止所尝试的: -
-- Declare few variables
DECLARE @Currentdate AS DATETIME
DECLARE @DueDate AS DATETIME
DECLARE @NewDate AS DATETIME
-- Set the variables properly, just for testing
SET @Currentdate = GETDATE()
SET @DueDate = DATEADD(MONTH, 2, DATEADD(YEAR, 1, @Currentdate))
-- Check the output
SELECT @Currentdate -- 2013-09-30 00:00:00.000
SELECT @DueDate -- 2014-11-30 00:00:00.000
所以,我想得到@NewDate
基于@Currentdate
年份的数据。为此,我尝试了:-
SELECT @NewDate = DATEADD(DAY, DAY(DATEDIFF(day, 1, @DueDate)), DATEADD(MONTH, DATEDIFF(MONTH, 0, @Currentdate), 0))
SELECT @NewDate -- 2013-09-30 00:00:00.000
但它没有奏效。:(
我的预期结果是:
-- 2013-11-30 00:00:00.000
-- Having the due date month and date same, but the year as current date one.
任何帮助表示赞赏!
更新
对我造成的所有混乱感到抱歉。我的问题简单来说是:-
我想获得一个新的日期变量,其日期和月份与@DueDate
变量相同,但年份与@Currentdate
变量中给出的相同。
我希望这会澄清一些事情。