如何在 MS SQL 中检索过去 30 分钟的记录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4471041/
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
How to retrieve records for last 30 minutes in MS SQL?
提问by Saravanan I M
I want to retrieve records for last 30 minutes in a table. How to do that? Below is my query..
我想检索表中过去 30 分钟的记录。怎么做?下面是我的查询..
select * from
[Janus999DB].[dbo].[tblCustomerPlay]
where DatePlayed < CURRENT_TIMESTAMP
and DatePlayed >
(CURRENT_TIMESTAMP-30)
回答by kemiller2002
Change this (CURRENT_TIMESTAMP-30)
改变这个 (CURRENT_TIMESTAMP-30)
To This: DateADD(mi, -30, Current_TimeStamp)
对此: DateADD(mi, -30, Current_TimeStamp)
To get the current date use GetDate().
要获取当前日期,请使用 GetDate()。
回答by Adriaan Stander
回答by John Pickup
Use:
用:
SELECT *
FROM [Janus999DB].[dbo].[tblCustomerPlay]
WHERE DatePlayed < GetDate()
AND DatePlayed > dateadd(minute, -30, GetDate())
回答by Jesse Novotny
DATEADD
only returned Function does not exist
on MySQL 5.5.53 (I know it's old)
DATEADD
仅Function does not exist
在 MySQL 5.5.53 上返回(我知道它很旧)
Instead, I found DatePlayed > DATE_SUB(CURRENT_TIMESTAMP, INTERVAL 30 minute)
to produce the desired result
相反,我发现DatePlayed > DATE_SUB(CURRENT_TIMESTAMP, INTERVAL 30 minute)
产生了想要的结果
回答by TheRealZing
Remember that CURRENT_TIMESTAMP - (number) works fine, but that you need to understand what number it is looking for - it is floating-point number of days. So CURRENT_TIMESTAMP-1.0 is 1 day ago, CURRENT_TIMESTAMP-0.5 is 1/2 day ago. For 30 minutes, that is 1.0/48.0 (use radix so result is a floating point number) or 0.0208333333333333, so your query will work if re-written as
请记住 CURRENT_TIMESTAMP - (number) 工作正常,但您需要了解它正在寻找的数字 - 它是浮点天数。所以 CURRENT_TIMESTAMP-1.0 是 1 天前,CURRENT_TIMESTAMP-0.5 是 1/2 天前。30 分钟,即 1.0/48.0(使用基数,因此结果是浮点数)或 0.0208333333333333,因此如果重写为,您的查询将起作用
select * from
[Janus999DB].[dbo].[tblCustomerPlay]
where DatePlayed < CURRENT_TIMESTAMP
and DatePlayed >
CURRENT_TIMESTAMP-1.0/48.0
You could also use 1.0/24.0/2.0 if that looks more like 1/2 hour to you.
如果对您来说更像是 1/2 小时,您也可以使用 1.0/24.0/2.0。
回答by Sir Wobin
SQL Server uses Julian dates so your 30 means "30 calendar days". getdate() - 0.02083 means "30 minutes ago".
SQL Server 使用儒略日期,因此您的 30 表示“30 个日历日”。getdate() - 0.02083 表示“30 分钟前”。