在T-SQL中将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'

有什么捷径吗?

谢谢!

应该可以:

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

总是得到第二天你可以做:

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

NextDay

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

Month From Today:

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

相关内容

  • 没有找到相关文章

最新更新