Avatar of erikTsomik
erikTsomik
Flag for United States of America asked on

sql check if the datetime in between 2 datetime

I have a  query and What I trying to do is to exclude the people that are taken in this time frame, but it still showing me a person where this person should be excluded

	SELECT DISTINCT U.userKey
		, U.firstName
	    , U.lastName
	    , U.email
	FROM users U
		
	WHERE U.enable = 'Y' and u.userKey=36749
		and  not EXISTS (
			SELECT su.instructorKey
			FROM Session S 
			INNER JOIN SessionUnit SU  ON S.SessionKey = SU.SessionKey
			inner join sessionMap SM  on SM.sessionKey= S.sessionKey
			WHERE
			 su.instructorKey = U.userKey
AND
(
'2015-09-01 08:01:00' between SU.sessionStart and SU.sessionEnd
OR '2015-09-01 09:59:00' between SU.sessionStart and SU.sessionEnd
OR SU.sessionStart between '2015-09-01 08:01:00' AND '2015-09-01 09:59:00'
OR SU.sessionEnd between '2015-09-01 08:01:00' AND '2015-09-01 09:59:00'
) 

) 

order by   U.lastName

Open in new window

Microsoft SQL Server 2005Microsoft SQL Server 2008SQL

Avatar of undefined
Last Comment
Vitor Montalvão

8/22/2022 - Mon
Guy Hengel [angelIII / a3]

this is the simple condition:

AND  SU.sessionStart < '2015-09-01 10:10:00'
AND SU.sessionEnd > '2015-09-01 08:00:00'

nothing else is needed
ASKER CERTIFIED SOLUTION
Vitor Montalvão

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23