SQL Server中的特定时间范围查询 [英] Specific Time Range Query in SQL Server
问题描述
我正在尝试查询特定的时间范围:
I'm trying to query a specific range of time:
- 即。 3/1/2009 - 3/31/2009
- 每天上午6点到10点之间
- 周二/周三/周四
我已经看到你可以获取特定范围的数据,但只能从头到尾,这更具体一些。我没有看到任何直接帮助我的SQL Server命令,还有其他人有什么想法可以形成这个吗?
I've seen that you can get data for a particular range, but only for start to end and this is quite a bit more specific. I didn't see any SQL Server commands that would directly help me on this, so does anybody else have any thoughts on how you would form this?
我已经看过这个,但我认为这不足够
I've seen this, but I don't think it's nearly specific enough for this range.
谢谢!
推荐答案
我假设你想要所有这三个作为选择标准的一部分。你需要在你的位置提供几个陈述,但它们将与您的问题所在的链接相似。
I'm assuming you want all three of those as part of the selection criteria. You'll need a few statements in your where but they will be similar to the link your question contained.
SELECT *
FROM MyTable
WHERE [dateColumn] > '3/1/2009' AND [dateColumn] <= DATEADD(day,'3/31/2009',1)
--make it inclusive for a datetime type
AND DATEPART(hh,[dateColumn]) >= 6 AND DATEPART(hh,[dateColumn]) <= 22
-- gets the hour of the day from the datetime
AND DATEPART(dw,[dateColumn]) >= 3 AND DATEPART(dw,[dateColumn]) <= 5
-- gets the day of the week from the datetime
希望有帮助。
这篇关于SQL Server中的特定时间范围查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!