Subtract hours from SQL Server 2012 query result

岁酱吖の 提交于 2019-12-06 23:51:56

问题


I am running queries on an alarm system signal automation platform database in SQL Server 2012 Management Studio, and I am running into some hiccups.

My queries run just fine, but I am unable to refine my results to the level that I would like.

I am selecting some columns that are formatted as DATETIME, and I simply want to take the value in the column and subtract 4 hours from it (i.e., from GMT to EST) and then output that value into the query results.

All of the documentation I can find regarding DATESUB() or similar commands are showing examples with a specific DATETIME in the syntax, and I don't have anything specific, just 138,000 rows with columns I want to adjust time zones for.

Am I missing something big or will I just need to continue to adjust manually after I being manipulating my query result? Also, in case it makes a difference, I have a read-only access level, and am not interested in altering table data in any way.


回答1:


Well, for starters, you need to know that you aren't restricted to use functions only on static values, you can use them on columns.

It seems that what you want is simply:

SELECT DATEADD(HOUR,-4,YourColumnWithDateTimes)
FROM dbo.YourTable



回答2:


Maybe it will be helpful

SELECT DATEPART(HOUR, GETDATE()); 

DATEPART docs from MSDN



来源:https://stackoverflow.com/questions/32895736/subtract-hours-from-sql-server-2012-query-result

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!