在SQL Server中获取20:00到05:00之间的数据

本文关键字:之间 数据 SQL 获取 Server | 更新日期: 2023-09-27 18:11:49

我在SQL Server中有一个表,其中包含一些数据和一个datetime列。

我想在20:00到05:00之间获取数据。

我有以下查询,但无法获得成功的

select 
    txt_target_cell_id, count(*) as 'Total Count', txt_longitude, txt_latitude
from 
    tbl_cdr_analyzer_load
where 
    DATEPART(hour, dat_start) between 20 and 05
group by 
    txt_target_cell_id, txt_longitude, txt_latitude

在SQL Server中获取20:00到05:00之间的数据

您的BETWEEN语句意味着:

DATEPART(hour, dat_start) >= 20 AND DATEPART(hour, dat_start)<=04

这总是错误的。

你可以试着这样做:

select 
    txt_target_cell_id,count(*) as 'Total Count',txt_longitude,txt_latitude
from 
    tbl_cdr_analyzer_load
where 
    DATEPART(hour, dat_start) <= 04 OR DATEPART(hour, dat_start)>=20
group by 
    txt_target_cell_id,txt_longitude,txt_latitude
select 
    txt_target_cell_id, count(*) as 'Total Count', txt_longitude, txt_latitude
from 
    tbl_cdr_analyzer_load
where 
    DATEPART(hour, dat_start) between 04 and 20 //Change here
group by 
    txt_target_cell_id, txt_longitude, txt_latitude

这是因为valu1和value2之间的id等效于。其中id>=value1并且id<值2获得期望结果,,,

Select 
    txt_target_cell_id,count(*) as 'Total Count',txt_longitude,txt_latitude
from 
    tbl_cdr_analyzer_load
where 
    DATEPART(hour, dat_start) <= 04 OR DATEPART(hour, dat_start)>=20
group by 
    txt_target_cell_id,txt_longitude,txt_latitude