首页 > 解决方案 > 在包含小时和分钟的日期部分上使用 between

问题描述

我有一个名为 Epoch 的时间字段,我知道如何从早上 6 点到 8 点提取数据。我不知道如何在 6:30 到 8:30 之间执行此操作,因为 datepart 是小时或分钟,而不是两者。这是我到目前为止的代码。

select tmc_code, avg(speed) as AVGS
from [dbo].[CEAR_FULL_NONULL_CARS_3_12] 
where epoch between (datepart(hour, epoch) = 6 and datepart(minute, epoch) = 30) and (datepart(hour, epoch) = 8 and datepart(minute, epoch) = 25)
group by TMC_Code order by tmc_code

标签: sql-server

解决方案


假设 Epoch 是日期时间,也许转换为TIME

Declare @YourTable table (epoch datetime)
Insert into @YourTable values 
 ('2020-01-22 07:22:18')
,('2020-01-22 10:45:00')

Select * 
 From  @YourTable 
 Where convert(time,epoch) between '06:30' and '08:30'

退货

 epoch
 2020-01-22 07:22:18.000

推荐阅读