如何使用时间戳减去特定小时数进行 SQL Server 选择

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4585604/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 08:44:54  来源:igfitidea点击:

How to do a SQL Server select using a timestamp minus a certain number of hours

sqlsql-servertime

提问by Elliott

Right now I have a SQL query that allows me to select entries in a table that have been inserted over the past day.

现在我有一个 SQL 查询,它允许我选择过去一天插入的表中的条目。

The query is:

查询是:

Select account from mytable where create_date > current_timestamp - 1   

But suppose that I wanted to select entries that had been inserted over the base two hours?

但是假设我想选择在两个小时内插入的条目?

How would I write that query?

我将如何编写该查询?

回答by AdaTheDev

You can use DATEADD, like so:

您可以使用DATEADD,如下所示:

SELECT account
FROM MyTable
WHERE create_date > DATEADD(hh, -2, GETDATE())

回答by brian

Consider that a timestamp and a datetime are extremely different in sql server. A timestamp is a unique binary number whereas a datetime is a date and time combination. I think you'll need datetime.

考虑到时间戳和日期时间在 sql server 中非常不同。时间戳是唯一的二进制数,而日期时间是日期和时间的组合。我想你需要约会时间。

回答by Jimmy

You could try along the lines of

你可以尝试沿着

Select account from mytable where create_date > current_timestamp - 0.083333333