简体   繁体   English

sql:其中日期包含12:00到22:00之间的时间

[英]sql : which dates contain time between 12:00 and 22:00

i have myTable with column RecTime AS DATETIME 我有myTableRecTime AS DATETIME

RecTime
-----------------------
2013-05-22 15:32:37.530
2013-05-22 22:11:16.103
2013-05-22 16:24:06.883
2013-05-22 16:38:30.717
2013-05-22 23:54:41.777
2013-05-23 22:01:00.000
2013-05-23 09:59:59.997

i need an SQL statement that tell me which dates contain time between 12:00 and 22:00 我需要一条SQL语句,告诉我哪些日期包含12:00到22:00之间的时间

expected result is :- 预期结果是:-

RecTime                 | foo
------------------------|-----
2013-05-22 15:32:37.530 |   1
2013-05-22 22:11:16.103 |   0
2013-05-22 16:24:06.883 |   1
2013-05-22 16:38:30.717 |   1
2013-05-22 23:54:41.777 |   0
2013-05-23 22:01:00.000 |   0
2013-05-23 09:59:59.997 |   0

for now i use the following :- 现在我使用以下内容:

SELECT 
    [RecTime]
    , CASE WHEN [RecTime] >= CONVERT(DATETIME, CONVERT(VARCHAR(4), DATEPART(YEAR, [RecTime])) + '-' + CONVERT(VARCHAR(2), DATEPART(MONTH, [RecTime])) + '-' + CONVERT(VARCHAR(2), DATEPART(DAY, [RecTime])) + ' 12:00')
            AND [RecTime] <= CONVERT(DATETIME, CONVERT(VARCHAR(4), DATEPART(YEAR, [RecTime])) + '-' + CONVERT(VARCHAR(2), DATEPART(MONTH, [RecTime])) + '-' + CONVERT(VARCHAR(2), DATEPART(DAY, [RecTime])) + ' 22:00')
        THEN 1
        ELSE 0
    END  
FROM dbo.myTable

and i know this is not the best solution / performance. 而且我知道这不是最好的解决方案/性能。

help would be appreciated 帮助将不胜感激

You've found DATEPART and yet the obvious didn't spring to mind: 您已经找到了DATEPART ,但显而易见的事情并没有引起您的注意:

CASE WHEN DATEPART(hour,[RecTime]) >= 12
     AND DATEPART(hour,[RecTime]) < 22
    THEN 1
    ELSE 0

As a side note, when working with a continuum like time, it's almost always more sensible to define semi-open intervals (where you use >= on the start and < on the finish), otherwise you end up with oddities such as 22:00:00.000 exactly being included in your period, and 22:00:00.003 being excluded. 作为边注,与像时间的连续工作的情况下,它几乎总是更明智的,以限定半开区间(其中使用>=上开始和<上完成的),否则你最终古怪如22:您的期间中正好包含00:00.000,而22:00:00.003已被排除。 That's rarely correct. 这很少是正确的。 I've adjusted my query to match this pattern. 我已调整查询以匹配此模式。


i know this is not the best solution / performance. 我知道这不是最好的解决方案/性能。

Performance is always going to be poor for this query because we'll never be able to leverage an index. 该查询的性能始终很差,因为我们永远无法利用索引。 If this is a frequent form of query (querying on just the Hour component), you might consider adding a computed column which you can then index. 如果这是一种常见的查询形式(仅在Hour组件上查询),则可以考虑添加一个计算列,然后可以对其进行索引。


To include 22:00:00.000 exactly, I'd handle that as a separate, specialised case: 确切地讲22:00:00.000,我将其作为一个单独的特殊案例处理:

CASE WHEN DATEPART(hour,[RecTime]) >= 12
     AND DATEPART(hour,[RecTime]) < 22
    THEN 1
     WHEN DATEPART(hour,RecTime) = 22 AND DATEPART(minute,RecTime) = 0 and DATEPART(second,RecTime) = 0 and DATEPART(millisecond,RecTime) = 0
    THEN 1
    ELSE 0

One more approach 另一种方法

SELECT RecTime, 
       CASE WHEN CAST(RecTime AS TIME) 
                 BETWEEN '12:00:00' AND '22:00:00' 
            THEN 1 ELSE 0 END foo
  FROM Table1

Output: 输出:

|                    RECTIME | FOO |
|----------------------------|-----|
| May, 22 2013 15:32:37+0000 |   1 |
| May, 22 2013 22:11:16+0000 |   0 |
| May, 22 2013 16:24:06+0000 |   1 |
| May, 22 2013 16:38:30+0000 |   1 |
| May, 22 2013 23:54:41+0000 |   0 |
| May, 23 2013 22:01:00+0000 |   0 |
| May, 23 2013 09:59:59+0000 |   0 |

Here is SQLFiddle demo 这是SQLFiddle演示

SELECT 
[RecTime],
(Select CASE 
     WHEN DATEPART(hour,[RecTime])  between 12 AND 22 Then 1 
else 0
END
From dbo.myTable ) as foo
 From dbo.myTable 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM