Between Operator 在 Time Field 上未按预期运行

Between Operator not functioning as expect on Time Field

我想从 rfq_create_date_time 中提取时间部分,而 select 只记录两次之间的时间。时间是格林威治标准时间,但我只对香港时区的记录感兴趣,即格林威治标准时间 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'.

有没有办法根据跨越午夜的时间字段 select 行?

尝试以下操作:

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

仅当您想在其他算术运算之前进行此计算但不适合日期时间运算时,才应包含大括号 因此

这是错误的rfq_create_time between ('23:00:00.0000000' and '10:00:00.0000000')

这是正确的rfq_create_time between '23:00:00.0000000' and '10:00:00.0000000'