I have following query:
x
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
Advertisement
Answer
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.