是否可以修改日期的日期部分而无需使用DateAdd或必须计算日期差异?
SET @ReturnDate = '5/16/2012'
SET @ModifyDay = 20
仅将16
修改为20
结果应为5/20/2012
原因是我正在将vb代码转换为存储过程:
Dim InvDate_Day_of_Month As Integer = CInt(Day(InvDate))
PaymentDate = DateAdd(DateInterval.Month, 1, InvDate)
PaymentDate = New Date(PaymentDate.Year, PaymentDate.Month, DayofTheMonth)
答案 0 :(得分:2)
SET @ReturnDate = @ReturnDate + (@ModifyDay - DATEPART(day, @ReturnDate))
答案 1 :(得分:1)
如果您只想在日期中添加4天,则可以直接添加4天:
DECLARE @ReturnDate DATETIME;
SET @ReturnDate = '5/16/2012'
SELECT @ReturnDate, @ReturnDate + 4
这选择 2012-05-16 00:00:00.000和 2012-05-20 00:00:00.000