SQL如何按时间段查询

2024-12-02 08:36:59
推荐回答(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