开发者

Sql query getting datetime range

开发者 https://www.devze.com 2023-03-24 01:53 出处:网络
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 o

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号