在T-SQL中将DateTime汇总到第二天?

时间:2013-03-07 21:38:03

标签: sql sql-server-2008 tsql datetime

我有一个日期,我希望第二天到期。

例如

SomeDate DATETIME,
NewDate DATETIME

SomeDate = '01/05/2012 8:00:0 PM'
NewDate = Get the next day value here (in this sample it would be '01/05/2012 8:00:0 PM' + 4 Hours which is '02/05/2012 00:00:00 AM')

所以简单来说,我想要的是:

NewDate = '02/05/2012 00:00:00 AM'

快点?

谢谢!

3 个答案:

答案 0 :(得分:4)

这应该有效:

SELECT DATEADD(Day, DATEDIFF(Day, 0, SYSDATETIME()), 1)

答案 1 :(得分:3)

总是第二天你可以做到:

SELECT DATEADD(day, 1, CAST(@date As Date))

如果您希望将DateTime值“舍入”到最接近的一整天:

SELECT 
   CASE WHEN CAST(@date As Time) < '12:00' THEN 
       CAST(@date As Date) 
   ELSE 
       DATEADD(day, 1, CAST(@date As Date))
   END

答案 2 :(得分:1)

NextDay

Select DateAdd(day, Datediff(day, 0, getdate()) +1, 0)

今天一个月:

Select Dateadd(month, 1, Dateadd(Day, Datediff(day, 0, getdate()), 0))