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?

-------------Problems Reply------------

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.

Category:sql server Views:0 Time:2011-07-29
Tags: sql server

Related post

  • T-SQL Query where DateTime Range WHERE conditions are Generate by Another Query 2011-07-06

    How do I write a T-SQL query where the filter conditions of datetime ranges are generated from another query. For Example Query A would Return: StartTime EndTime 2011-07-06 04:05:42.137 2011-07-06 04:05:58.503 2011-07-06 04:25:51.103 2011-07-06 04:26

  • SQL query and datetime parameter takes long time to execute 2009-10-29

    I have a query which takes datetime as a parameter, what we have observed is that if you supply datetime parameter through a variable, Query takes 2 -3 times more time to execute than if you directly hardcode the parameter, Is there any reason or sol

  • Sql query to select range of dates in a single column 2011-08-11

    I have written an SQL query to return the total quantity of products ordered and total price. The query returns a result for each order in the range I have specified. Here's the output ( to enlarge): Please advise. My SQL query is as follows: SELECT

  • Sql Query with Date range 2012-03-20

    I am trying to fetch records from the table using the below sql query. SELECT Code,Description FROM Table WHERE ID= 1 AND FromDate >= '2010-02-14' AND ToDate <= '2012-03-14' Even though records exists for this date, query returns nothing. ID HI

  • SQL Query and date range 2014-01-16

    The following query date range works well. SELECT tbl_WS.Workshop, tbl_WS.WCode, tbl_WS.Type, tbl_WS.Credits, tbl_WS.Date, tbl_WS.StartTime, tbl_WS.Campus, tbl_WS.LO1, tbl_WS.LO2, tbl_WS.LO3, tbl_WS.LO4, tbl_WS.LO5, tbl_WS.LO6, tbl_Students.SNum, tbl

  • T-SQL query with date range 2010-05-19

    I have a fairly weird 'bug' with a simple query, and I vaguely remember reading the reason for it somewhere a long time ago but would love someone to refresh my memory. The table is a basic ID, Datetime table. The query is: select ID, Datetime from T

  • Need help with a SQL query selecting date ranges from a table of period quarters 2010-07-07

    I have a table called Periods that looks like this PeriodID | PeriodYear | PeriodQuarter 7 | 2009 | 1 8 | 2009 | 2 9 | 2009 | 3 10 | 2009 | 4 11 | 2010 | 1 12 | 2010 | 2 Each row in the table represents 1 of the 4 quarters of the year (like 3-monthly

  • SQL Query for Date Range, multiple start/end times 2010-07-20

    A table exists in Microsoft SQL Server with record ID, Start Date, End Date and Quantity. The idea is that for each record, the quantity/total days in range = daily quantity. Given that a table containing all possible dates exists, how can I generate

  • Querying a datetime range while taking into account the time zone 2011-05-13

    I'm having a lot of trouble with a query. I don't really know how to explain this well, but I'm going to try. Basically, we have several objects with a 'posted_at' field that keeps the date and time something was posted, with the time zone, in a date

  • how to sql query a named range on a worksheet with spaces in the name 2013-05-19

    I have a workbook with multiple sheets, and each sheet has the same set of named ranges (IE they are scoped to the sheet, not workbook). I want to query based on a named range on any of the sheets. Some sheets have names with no spaces, and others do

  • Sql query to get range of values 2009-04-03

    I have a table called TimeList SlotID SlotStartTime SlotEndTime (int identity) (varchar(10)) (varchar(10)) 1 8:00AM 8:15AM 2 8:15AM 8:30AM 3 8:30AM 8:45AM 4 8:45AM 9:00AM 5 9:00AM 9:15AM 6 9:15AM 9:30AM 7 9:30AM 9:45AM 8 9:45AM 10:00AM If I am passin

  • SQL query optimization (CTE + range function) in SQL Server 2005 2009-08-06

    I'm wondering whether such query could be potentially optimized. I've hugely simplified it, and you see the core of it. with Rec (Id,Name,ParentId) as ( select Id,Name,ParentId from Departments where ParentId is null union all select d.Id, d.Name, d.

  • Sql Query Group a range of numbers with low's and high's 2009-10-08

    I have a table that looks something like below street_id | address_number | address_direction | address_street | address_type ... ---------------------------------------------------------------------------------- 1 | 121 | W | Fake | St 1 | 131 | W |

  • Ruby on Rails query by datetime range ( last 24, 48, etc- hours ) 2010-01-28

    I'm trying to build a query that will search for recent entries based on column 'last_login_at'. This is a datetime field with time zone (i.e. Time.zone.now) When I execute User.find(:all, :conditions => ["last_login_at < ?", 24.hours.ago]) I g

  • Ignoring the year in SQL Query with date range 2012-02-28

    Normally if I want to make a query on a table by date range I'll do it this way: SELECT DISTINCT c.ID AS 'id' FROM CUST c JOIN TICKET t ON s.ID = t.SALE_ID WHERE c.ACTIVE_IND = 1 AND t.DELIV_DATE BETWEEN '01-01-2012' AND '01-02-2012' ORDER BY t.DELIV

  • how to apply sql query with a range of dates on crystal report 2012-04-11

    I have to retrieve of how many an employee attend to work between a range of two date. I managed to get the query like this ex date range : 2012-02-03 to 2012-03-03 SELECT A.EmployeeD, A.EmployeeName, ISNULL(SUM(CASE WHEN a.AttendanceDate BETWEEN '20

  • SQL Query for datetime within 5 minutes of each other 2011-05-03

    I am trying to set up a report that queries for data within five minutes of each other. This way, I can recognize that one of my machines is down. This is what I have so far: SELECT A_M_DEVICE.M_Device_ID, A_M_DEVICE.Machine_Name, A_M_DEVICE.IP_Addre

  • SQL Query which returns a table where each row represents a date in a given range 2008-10-03

    Is it possible to create a SQL query which will return one column which contains the dates from a given date range (e.g. all dates from last year till today). E.g. dat ---- 2007-10-01 2007-10-02 2007-10-03 2007-10-04 ... I am wondering if this is pos

  • How do i Find a related record matching a date range using MS SQL query 2009-09-09

    I have a table of PricePlan that looks like Table PricePlan ID MerchantID NAME VALIDFROM VALIDUPTO 1. M1 Plan A 1-sep-09 30-sep-09 2. M1 Plan B 7-sep-09 21-sep-09 3. M2 Plan Da 1-sep-09 30-Sep-09 Given a @FromDate and @ToDate I need to find the match

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.080 (s). 11 q(s)