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.
谢谢!
推荐答案
我假设您希望将所有这三个作为选择标准的一部分.您需要在 where 中添加一些语句,但它们将类似于您的问题所包含的链接.
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,1,'3/31/2009')
--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屋!