怎样在SQL的datetime类型数据中加减年, 月, 日, 时, 分, 秒?

在排查SharePoint问题的时候, 经常需要到SharePoint的数据库中进行查询, 有时候就需要对时间进行一些操作.

比如说, 在排查Alert的问题的时候, 经常需要用到下面的一句查询:

Select ItemName, EventData, ACL, EventTime
from EventCache 
where (ACL is not null) and 
    (EventData is not null)
order by eventtime desc

但是这里的EventTime是GMT时间, 对于我们中国来说, 看起来不太直观, 因为我们使用的是GMT+8的北京时间. 可以使用下面的语句来对结果进行更正.

Select ItemName, EventData, ACL, DATEADD(HOUR, 8, EventTime)
from EventCache 
where (ACL is not null) and 
    (EventData is not null)
order by eventtime desc

至于要修改年, 月, 日什么的, 都可以在下面的文档中找到答案.

DATEADD (Transact-SQL)

http://msdn.microsoft.com/en-us/library/ms186819.aspx

原文地址:https://www.cnblogs.com/awpatp/p/1737836.html