Oracle 按照时段进行筛选查询符合条件SQL语句

时间:2022-08-10 02:57:03

--
Crea1te tablecreate table TESTDB
(
ID NVARCHAR2(
20),
INTIME DATE,
/*进入时间*/
OUTTIME DATE
/*出去时间*/
)
tablespace LBSBUS
pctfree
10
pctused
40
initrans
1
maxtrans
255
storage
(
initial 64K
next 1M
minextents
1
maxextents unlimited
);

插入测试数据
insert into TESTDB (ID, INTIME, OUTTIME)
values ('1', to_date('13-05-2017 10:58:58', 'dd-mm-yyyy hh24:mi:ss'), to_date('13-05-2017 15:58:58', 'dd-mm-yyyy hh24:mi:ss'));

insert into TESTDB (ID, INTIME, OUTTIME)
values ('2', to_date('16-05-2017 20:58:59', 'dd-mm-yyyy hh24:mi:ss'), to_date('16-05-2017 23:58:59', 'dd-mm-yyyy hh24:mi:ss'));

insert into TESTDB (ID, INTIME, OUTTIME)
values ('3', to_date('18-05-2017 21:58:59', 'dd-mm-yyyy hh24:mi:ss'), to_date('18-05-2017 22:58:59', 'dd-mm-yyyy hh24:mi:ss'));

insert into TESTDB (ID, INTIME, OUTTIME)
values ('4', to_date('19-06-2017 02:58:59', 'dd-mm-yyyy hh24:mi:ss'), to_date('19-06-2017 07:58:59', 'dd-mm-yyyy hh24:mi:ss'));

insert into TESTDB (ID, INTIME, OUTTIME)
values ('5', to_date('20-06-2017 03:59:00', 'dd-mm-yyyy hh24:mi:ss'), to_date('20-06-2017 08:59:00', 'dd-mm-yyyy hh24:mi:ss'));

筛选出0点到早上8点之间进入的所有记录

select * from TESTDB
where to_char(INTIME,'hh24:mi:ss') between '00:00:00'
and '08:00:00'

 查询结果如下:

Oracle 按照时段进行筛选查询符合条件SQL语句