在SQL中两次之间获取所有日期值?

时间:2018-10-25 09:04:11

标签: sql sql-server

表1:

emp_id           in_time              out_time
9999    2018-10-21 08:00:00.000   2018-10-22 06:00:00.000
9999    2018-10-22 06:00:00.000   2018-10-22 07:00:00.000
9999    2018-10-23 19:00:00.000   2018-10-24 07:00:00.000

表2:

select a.emp_reader_id, DT, EVENTID1
from trnevents a 
left outer join daily_attendance_data d on d.emp_reader_id = a.emp_reader_id
left outer join employee e on e.emp_reader_id = a.emp_reader_id
where (CONVERT(VARCHAR(26), A.DT, 23) >= CONVERT(VARCHAR(26), '2018-10-23', 23) 
       and CONVERT(VARCHAR(26), A.DT, 23) <= CONVERT(VARCHAR(26), '2018-10-24', 23))
  and a.emp_reader_id=9999
group by a.emp_Reader_id, a.dt,a.eventid
order by emp_reader_id, DT asc

我尝试通过该查询从table2的intime和outtime之间获取table1中的所有数据,但是我无法在这些in_time和out_time之间获取

2018-10-23 19:00:00.000
2018-10-24 03:00:00.000
2018-10-24 05:00:00.000
2018-10-24 07:00:00.000
  

预期输出:       in_time:2018-10-23 19:00:00.000       out_time:2018-10-24 07:00:00.000

DeviceId

1 个答案:

答案 0 :(得分:2)

如果DT是日期时间,则在此条件下工作无需任何varchar转换

where DT>='2018-10-23 19:00:00.000' and DT<='2018-10-24 07:00:00.000'