SQL如何按时间段查询

2024年12月02日 08:36
有4个网友回答
网友(1):

SELECT * FROM Record WHERE
TO_CHAR(Times,'YYYY/MM/DD') BETWEEN '2010/01/01' and '2010/12/31'
AND TO_CHAR(Times,'D') BETWEEN 2 AND 6
AND (TO_CHAR(Times,'HH24:Mi:SS') BETWEEN '08:30:00' and '12:00:00' or TO_CHAR(Times,'HH24:Mi:SS') BETWEEN '12:30:00' and '17:00:00')
'D' 是获取星期,从星期日 开始 是1 ,其他的就不用说了吧。

网友(2):

估计你这个是sqlserver,我给你写一下:

SELECT * FROM Record
WHERE
convert(varchar(10),Times,120) BETWEEN '2010-01-01' and '2010-12-31'
AND DatePart('w',Times) BETWEEN 2 AND 6
AND (
CONVERT(varchar(8), Times, 8) BETWEEN '08:30:00' and '12:00:00'
or
CONVERT(varchar(8), Times, 8) BETWEEN '12:30:00' and '17:00:00'
)

网友(3):

时间函数各个数据库不完全相同,但思路是一样的,不要纠结时间段。
其实你上面的需求就是:
YEAR(RECORD)=2010
DAYOFWEEK BETWEEN 1 AND 5
这个样子,就是时间型字段拆分判断,根据不同数据库使用时间函数就可以了。

网友(4):

用 between and