sql-server - 在包含小时和分钟的日期部分上使用 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
解决方案
假设 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
推荐阅读
- mysql - MySql - 使用选择更新
- javascript - discord js 机器人硬币系统| sqlite 错误
- php - Mysqli bind_param 用于更新查询中的列名
- rest-assured - 如何在使用放心发送 API 时更新请求
- javascript - Revert `display: none` on elements to original value
- sqlite - SQlite 将一个链接到另一个表的多个 ID
- c++ - C ++像从另一个线程一样锁定互斥锁?
- mysql - Mysql 存储函数 RETURN CAST(finNumber AS DECIMAL(12,2)) AS final 出现未知错误;
- list - 遍历列表中的元组并获取其值
- docker - docker compose 在负载容器上连接 ssh 隧道服务器