SQL QUERY ( SQL SERVER) Date & Time Difference

I have a SQL SERVER Table which has only one field "StartDate" the records are as follows

** 2011-07-28 19:30:00.000

2011-07-29 21:50:00.000

2011-07-25 09:20:00.000 **

What i want to do is :

SHOW RECORDS if its CURRENT DATE ( todays date ) and the time difference between current time the StartDate is not less then 5 minutes, i have written the following code but it doesnt show me the time difference ?

SELECT * FROM table WHERE DATEDIFF(day, StartDate, GETDATE()) <= 0
This question and answers originated from www.stackoverflow.com
Question by (7/28/2011 10:46:05 AM)

Answer

SELECT StartDate
FROM table
WHERE YEAR(StartDate)=YEAR(GETDATE())
AND MONTH(StartDate)=MONTH(GETDATE())
AND DAY(StartDate)=DAY(GETDATE())
AND (DATEDIFF(minute, StartDate, GETDATE()) >= 5
     OR
     DATEDIFF(minute, StartDate, GETDATE()) <= 5) 
Answer by

Find More Answers
Related Topics  sql-server  sql-server-2005  tsql
Related Questions
  • Query by Date/Time in SQL Server 2005

    I have such a simple question, I feel stupid for asking it. I can't believe I'm hitting my head on this. I have a table called "Orders". "Orders" has a smalldatetime field called "DateOrdered". I…
  • SQL server 2005 - query aribitrary time interval in date range

    I have a DateTime column. I want to extract all records, lets say, from 8:30 to 16:15 within a certain date range. My problem is that I need to compare hour and minute as a single time value. I can …
  • Time Slot SQL Query

    I'm trying to write a query to see if an engineer visited his job in a agreed time slot. This is my query so far: SELECT v.[VISITDATE], CONVERT(VARCHAR, v.[STARTTIME], 105) AS 'Startdat…
  • SQL Server query to group sequential date data

    I have got a bit of 'brain fade' going on this afternoon, so if anyone can help with this mssql query it would be fantastic. I have a table called 'seasons' with three columns (there are more but…
  • SQL Server 2005 Query

    I just can't get my head around this today - so your help is much appreciated. Table Structure Create Table #trans ( TransactionId int, AccountNumber varchar(10), TransactionAmount money, Tran…
  • SQL Server Query Time Testing

    In SSMS, I may run a query that takes a while to run. If I run the query again, it completes almost instantly, making it difficult to see improvement or not when I make modifications like add a new …
  • Date difference in SQL Server 2005

    Let's say I have two dates in my table, DueDate and PaidDate. What I need to do is calculate the difference between those two dates. However, if the DateDiff() returns less than 0, it should show 0.…
  • SQL Server 2005 Date Time stamp Query

    One of my columns type is DateTime (Date Registered). I cannot create a query that filters all the data for eg. All registrations who registered on the 22/10/2008 between 18:00 and 20:00. Thanks
  • Date & time query question (SQL Server 2008)

    I have table that contain date and time field. id|date|time ========= 1|01/01/2001|10:45 2|01/02/2002|11:45 3|01/03/2003|12:45 4|01/04/2004|12:55 I need to know the difference between the MAX…
  • SQL server simple query

    How to select value from a table if it value is not null,and select a default value if not?.I.E: select coalesce(username,'its null') from tb_user where int_id_user = 0 I try to run this quer…