从当前日期时间减去小时数并与数据类型为 datetime 的列值进行比较
我想要过去一小时内添加到表中的所有 MAFN
列值 (:mm)。 ReturnDt
列存储以 DateTime 格式添加的时间,例如'2005-01-11 08:50:24.767'
我可以获取 ReturnDt
的小时数并获取当前时间(在两个单独的查询中)。
SELECT ReturnDt,
DATEPART(hour, ReturnDt) As
FROM [Sidney].[dbo].[LibraryTransactions]
但
SELECT GETDATE() 'Today', DATEPART(hh,GETDATE()) 'Hour Part'
由于显而易见的原因,这还不够。 首先,SELECT GETDATE()...
不能用作子查询。 其次,我似乎无法从 SELECT GETDATE() 查询的结果中减去 1 小时。 第三,即使上述两者以某种方式起作用,查询也会将时间减少到几个小时,并且不考虑日期。
我正在使用 SQl Server 2005
提前致谢!
编辑:
I want all the MAFN
column values that were added to the table in the last hour(:mm). The column ReturnDt
stores the time it was added in DateTime format ex.'2005-01-11 08:50:24.767'
I can get the hours for ReturnDt
and get the current hour(in two separate queries).
SELECT ReturnDt,
DATEPART(hour, ReturnDt) As
FROM [Sidney].[dbo].[LibraryTransactions]
And
SELECT GETDATE() 'Today', DATEPART(hh,GETDATE()) 'Hour Part'
But this is not enough for obvious reasons.
Firstly SELECT GETDATE()...
doesn't work as a subquery.
Secondly, I can't seem to subtract 1 hour from the result of the SELECT GETDATE()
query.
Thirdly, even if the above two somehow worked, the queries reduce time to hours and it doesn't take into account the date.
I am using SQl Server 2005
Thanks in advance!
Edit:
Expected Result: If I ran the query today(18 Mar 2022) at 16:00 hours I should only get these values
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一个小技巧...
将其放入您的 where caluse 中:
(column > getdate() - (1.0/24) AND column < getdate() - (2.0/24))
示例:(表 'sometable' 包含名为 'datecolumn' 的日期时间列)
select * from sometable
其中 (datecolumn > getdate() - (1.0/24) AND datecolumn < getdate() - (2.0/24))
...将为您提供某个表中“现在”1 到 2 小时老亲戚的记录。
A little trick...
Put this in your where caluse:
(column > getdate() - (1.0/24) AND column < getdate() - (2.0/24))
Example: (table 'sometable' contains a datetime column named 'datecolumn')
select * from sometable
where (datecolumn > getdate() - (1.0/24) AND datecolumn < getdate() - (2.0/24))
...will give you records in sometable who are between 1 and 2 hrs old relative "now".
DateDiff() 怎么样
How about DateDiff()