从 SQL Server 2012 查询结果中减去小时数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32895736/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Subtract hours from SQL Server 2012 query result
提问by adalious
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.
我正在 SQL Server 2012 Management Studio 中对报警系统信号自动化平台数据库运行查询,但遇到了一些问题。
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 thatvalue into the query results.
我选择了一些格式为 的列,DATETIME
我只想获取列中的值并从中减去 4 小时(即从 GMT 到 EST),然后将该值输出到查询结果中。
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.
我能找到的所有有关DATESUB()
或类似命令的文档都显示了具有特定DATETIME
语法的示例,而我没有任何具体内容,只有 138,000 行和我想要调整时区的列。
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.
我是否遗漏了一些重要的东西,或者我是否只需要在操纵查询结果后继续手动调整?此外,万一它有所不同,我有一个只读访问级别,并且对以任何方式更改表数据不感兴趣。
回答by Lamak
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
回答by Сергей Перекрестов
Maybe it will be helpful
也许它会有所帮助
SELECT DATEPART(HOUR, GETDATE());