Using between on a datepart with both hour and minute

Viewed 47

I have a time field called Epoch that I know how to pull data from 6 to 8 AM out of. I do not know how to do that for 6:30 to 8:30, since datepart is hour or minute not both. Here is the code I have thus far.

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
1 Answers

Assuming Epoch is a datetime, perhaps convert to a 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'

Returns

 epoch
 2020-01-22 07:22:18.000
Related