# 样例数据准备

CREATE TABLE EXAM (
OPDT DATETIME,
SM   VARCHAR(20)
)
GO
WITH EX (opdt,SM) AS
(
select '2019-02-01 07:29:59','不符' union all
select '2019-02-01 07:30:00','符合' union all
select '2019-02-01 07:30:01','符合' union all
select '2019-02-01 09:59:59','符合' union all
select '2019-02-01 10:00:00','符合' union all
select '2019-02-01 10:00:01','不符' union all
select '2019-02-02 07:29:59','不符' union all
select '2019-02-02 07:30:00','符合' union all
select '2019-02-02 07:30:01','符合' union all
select '2019-02-02 09:59:59','符合' union all
select '2019-02-02 10:00:00','符合' union all
select '2019-02-02 10:00:01','不符' union all
select '2019-12-01 07:29:59','不符' union all
select '2019-12-01 07:30:00','符合' union all
select '2019-12-01 07:30:01','符合' union all
select '2019-12-01 09:59:59','符合' union all
select '2019-12-01 10:00:00','符合' union all
select '2019-12-01 10:00:01','不符' union all
select '2019-12-02 07:29:59','不符' union all
select '2019-12-02 07:30:00','符合' union all
select '2019-12-02 07:30:01','符合' union all
select '2019-12-02 09:59:59','符合' union all
select '2019-12-02 10:00:00','符合' union all
select '2019-12-02 10:00:01','不符'
)
INSERT INTO EXAM SELECT * FROM EX



# 方法一

SELECT *
from EXAM
where opdt>='2019-01-01' and opdt<='2019-12-31'
and convert(varchar(20),opdt,108) >= '07:30:00'
and convert(varchar(20),opdt,108) <= '10:00:00'


# 方法二

WITH EX (opdt,SM) AS
(
select '2019-02-01 07:29:59','不符' union all
select '2019-02-01 07:30:00','符合' union all
select '2019-02-01 07:30:01','符合' union all
select '2019-02-01 09:59:59','符合' union all
select '2019-02-01 10:00:00','符合' union all
select '2019-02-01 10:00:01','不符' union all
select '2019-02-02 07:29:59','不符' union all
select '2019-02-02 07:30:00','符合' union all
select '2019-02-02 07:30:01','符合' union all
select '2019-02-02 09:59:59','符合' union all
select '2019-02-02 10:00:00','符合' union all
select '2019-02-02 10:00:01','不符' union all
select '2019-12-01 07:29:59','不符' union all
select '2019-12-01 07:30:00','符合' union all
select '2019-12-01 07:30:01','符合' union all
select '2019-12-01 09:59:59','符合' union all
select '2019-12-01 10:00:00','符合' union all
select '2019-12-01 10:00:01','不符' union all
select '2019-12-02 07:29:59','不符' union all
select '2019-12-02 07:30:00','符合' union all
select '2019-12-02 07:30:01','符合' union all
select '2019-12-02 09:59:59','符合' union all
select '2019-12-02 10:00:00','符合' union all
select '2019-12-02 10:00:01','不符'
)
select *
from EX
where opdt>='2019-01-01' and opdt<='2019-12-31'
and DATEPART(hh,opdt)*10000+DATEPART(mi,opdt)*100+DATEPART(ss,opdt) >= 073000
and DATEPART(hh,opdt)*10000+DATEPART(mi,opdt)*100+DATEPART(ss,opdt) <= 100000



# 方法三

WITH EX (opdt,SM) AS
(
select '2019-02-01 07:29:59','不符' union all
select '2019-02-01 07:30:00','符合' union all
select '2019-02-01 07:30:01','符合' union all
select '2019-02-01 09:59:59','符合' union all
select '2019-02-01 10:00:00','符合' union all
select '2019-02-01 10:00:01','不符' union all
select '2019-02-02 07:29:59','不符' union all
select '2019-02-02 07:30:00','符合' union all
select '2019-02-02 07:30:01','符合' union all
select '2019-02-02 09:59:59','符合' union all
select '2019-02-02 10:00:00','符合' union all
select '2019-02-02 10:00:01','不符' union all
select '2019-12-01 07:29:59','不符' union all
select '2019-12-01 07:30:00','符合' union all
select '2019-12-01 07:30:01','符合' union all
select '2019-12-01 09:59:59','符合' union all
select '2019-12-01 10:00:00','符合' union all
select '2019-12-01 10:00:01','不符' union all
select '2019-12-02 07:29:59','不符' union all
select '2019-12-02 07:30:00','符合' union all
select '2019-12-02 07:30:01','符合' union all
select '2019-12-02 09:59:59','符合' union all
select '2019-12-02 10:00:00','符合' union all
select '2019-12-02 10:00:01','不符'
)
select *
from EX
where opdt>='2019-01-01' and opdt<='2019-12-31'
and substring(convert(varchar(20),opdt,108),12,8)>='07:30:00'
and substring(convert(varchar(20),opdt,108),12,8)<='10:00:00'



04-28 5310

02-12 3804

10-08 6044

11-19 200

10-27 36

07-19

09-05 3919

12-25 6万+

08-02 5160

11-01 2184

#### 在xshell上查看某个时间段的日志

©️2020 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。