在操作员之间无法按时工作

时间:2018-07-06 02:31:49

标签: sql sql-server time between

我想从rfq_create_date_time中提取时间部分,只选择两次之间的记录。时间是格林尼治标准时间,但我只对香港时区的记录感兴趣,即格林尼治标准时间23.00至格林尼治标准时间10.00。

created_date_time       rfq_create_date_time    rfq_create_time
2018-06-28 21:04:28.637 2018-06-28 00:04:52.000 00:04:52.0000000
2018-06-21 00:08:09.110 2018-06-21 00:08:03.000 00:08:03.0000000
2018-06-05 00:18:28.853 2018-06-05 00:18:27.000 00:18:27.0000000
2018-06-05 00:31:51.110 2018-06-05 00:29:21.000 00:29:21.0000000
2018-06-27 21:04:39.713 2018-06-27 00:36:29.000 00:36:29.0000000
2018-06-22 00:37:39.650 2018-06-22 00:37:08.000 00:37:08.0000000
2018-06-22 21:04:21.427 2018-06-22 00:39:01.000 00:39:01.0000000
2018-06-01 00:42:59.297 2018-06-01 00:40:29.000 00:40:29.0000000
2018-06-22 21:04:21.427 2018-06-22 00:40:56.000 00:40:56.0000000
2018-06-19 21:04:24.093 2018-06-19 00:41:26.000 00:41:26.0000000
2018-06-21 21:04:30.197 2018-06-21 00:42:04.000 00:42:04.0000000
2018-06-14 00:43:28.013 2018-06-14 00:43:25.000 00:43:25.0000000
2018-06-01 00:46:13.280 2018-06-01 00:43:43.000 00:43:43.0000000

时间字段已成功创建(rfq_create_time):

SELECT
   *
FROM
(
    select      
                R.created_date_time,
                R.rfq_create_date_time,
                cast(R.rfq_create_date_time as time) as rfq_create_time,
    from        [ecomm_rfq].[dbo].[RFQ]                 R   WITH (NOLOCK) 
                join [ecomm_rfq].[dbo].[RFQ_RFQLEG]     L   WITH (NOLOCK)   
                on R.unique_id = L.unique_id                --inner join
    where
                R.rfq_create_date_time >= '2018-06-01'
                and R.rfq_create_date_time <= '2018-06-30'          
)       as innerTable

WHERE rfq_create_time between ('23:00:00.0000000' and '10:00:00.0000000')
order by rfq_create_time

between操作符抛出以下错误:

Incorrect syntax near the keyword 'and'.

是否有一种方法可以基于跨午夜的“时间”字段选择行?

2 个答案:

答案 0 :(得分:2)

尝试以下操作:

SELECT
   *
FROM
(
    select      
                R.created_date_time,
                R.rfq_create_date_time,
                cast(R.rfq_create_date_time as time) as rfq_create_time,
    from        [ecomm_rfq].[dbo].[RFQ]                 R   WITH (NOLOCK) 
                join [ecomm_rfq].[dbo].[RFQ_RFQLEG]     L   WITH (NOLOCK)   
                on R.unique_id = L.unique_id                --inner join
    where
                R.rfq_create_date_time >= '2018-06-01'
                and R.rfq_create_date_time <= '2018-06-30'          
)       as innerTable

WHERE rfq_create_time not between '10:00:00.0000000' and '23:00:00.0000000'
order by rfq_create_time

答案 1 :(得分:2)

Braces should be included only if u want to make this computation in prior to additional arithmetic operations but not prefered for datetime operations Hence

this is wrong rfq_create_time between ('23:00:00.0000000' and '10:00:00.0000000')

This is correct rfq_create_time between '23:00:00.0000000' and '10:00:00.0000000'