Sql query getting datetime range
Hi i 开发者_如何学编程am trying to write a query but I am unsure on how to do it. This is the scenario, i want to bring back records which were inserted into the database 30 minutes BEFORE the start of a specific time and 2 hours AFTER. This is what my query looks like;
SELECT Comment.AddDate,Event.StartTime
FROM Comment
JOIN Users on Users.USERS_ID = Comment.UserID
JOIN Event ON Users.USERS_ID = Event.UserID
WHERE EventID = 5630
Now from the above i need records which Comment.AddDate was entered 30 minutes before Event.StartTime and 2 hours later. How do i go about doing this?
SELECT Comment.AddDate, Event.StartTime
FROM Comment
JOIN Users ON Users.USERS_ID = Comment.UserID
JOIN Event ON Users.USERS_ID = Event.UserID
WHERE EventID = 5630
AND Comment.AddDate BETWEEN DATEADD(minute, -30, Event.StartTime)
AND DATEADD(hour, 2, Event.StartTime)
Documentation located here.
精彩评论