简体   繁体   中英

SQL Statement // between with datetime

I have following query:

select s.ScreenID, s.ScreenName, ps.ScheduleDate
from Screens s
left join PerformanceSchedules ps on ps.ScreenID = s.ScreenID
where s.TheatreID = 2 and ((ps.ScheduleDate not between convert(datetime, '2019-08-02 14:00:00.000') and convert(datetime, '2019-08-02 20:00:00.000')) or (ps.ScheduleDate is null))

The result of the query is:

ScreenID    ScreenName  ScheduleDate
4           Screen 1    2019-08-02 15:00:00.000
5           Screen 2    2019-08-02 15:00:00.000
6           Screen 3    NULL

Normally it should only return Screen 3 and not the other two, because the ScheduleDate of the two is in the time span.

The result should be:

6   Screen 3    NULL

I suspect you really want:

select s.ScreenID, s.ScreenName, ps.ScheduleDate
from Screens s left join
     PerformanceSchedules ps
     on ps.ScreenID = s.ScreenID and
        ps.ScheduleDate between convert(datetime, '2019-08-02 14:00:00.000', 121) and convert(datetime, '2019-08-02 20:00:00.000', 121)
where s.TheatreID = 2 and
      ps.ScreenID is null;

This will return the screens that have no schedule date between those times.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM