从 SQL Server 2012 查询结果中减去小时数

2022-01-03 00:00:00 datetime sql sql-server sql-server-2012

我正在 SQL Server 2012 Management Studio 中对报警系统信号自动化平台数据库运行查询,但遇到了一些小问题.

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.

我选择了一些格式为 DATETIME 的列,我只想获取列中的值并从中减去 4 小时(即从 GMT 到 EST),然后输出 那个 值到查询结果中.

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.

我能找到的关于 DATESUB() 或类似命令的所有文档都显示了在语法中带有特定 DATETIME 的示例,我没有任何特定的内容,只有 138,000 行,其中包含我想要调整时区的列.

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.

推荐答案

好吧,对于初学者来说,您需要知道您不仅可以在静态值上使用函数,还可以在列上使用它们.

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.

看来你想要的很简单:

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

相关文章