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(date) and the MIN(date)

And the MAX(time) and the MIN(time)

Something like.... MAX(date)-MIN(date) ???.....

Thanks in advance

This question and answers originated from www.stackoverflow.com
Question by (3/19/2010 8:15:49 AM)

Answer

DATEDIFF is your friend.

To get the difference in number of days: SELECT DATEDIFF(day, MIN(date), MAX(date)) FROM [table] = 1186

To get the difference in number of hours: SELECT DATEDIFF(hour, MIN(time), MAX(time)) FROM [table] = 2

To get the difference in number of minutes: SELECT DATEDIFF(minute, MIN(time), MAX(time)) FROM [table] = 130

Answer by

Find More Answers
Related Topics  sql-server-2008  query
Related Questions
  • Problem with Date Query - sql server 2008

    i have this date's & Time's: date1: 10/04/2010 - Time: 08:09 date2: 11/04/2010 - Time: 08:14 i need to show all the date's between 10/04/2010 time 06:00 and 11/04/2010 time 6:00 i wr…
  • combine 2 query - sql server 2008 question

    I have this query: select count (convert(varchar(50), TmpDate, 103 )),convert(varchar(50), TmpDate, 103 ) from MEN group by TmpDate order by TmpDate desc and I need to count how many rows it…
  • Sql Server 2008 Query

    I have a table which contains UserId, ItemId and ItemName columns. I want to get some user's different items if that users have all items that specific user's items. How can I do that with sql serve…
  • SQL Server Query Question

    Running SQL Server 2008, and I am definitely a new SQL user. I have a table that has 4 columns: EmpNum, User, Action, Updatetime A user logs into, and out of a system, it is registered in …
  • 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
  • SQL Server 2008 dynamic query doubt

    Possible Duplicates: What is the purpose of system table table master..spt_values and what are the meanings of its values? Why (and how) to split column using master..spt_values? I …
  • Group by query and SQL Server 2008

    I have this query select SUM(Qty) as Qty from WorkTbl group by Status having Status = 'AA' or Status = 'BB' this query returns 2 rows (100 and 500) How to sum those 2 rows ?
  • Help with random - sql server 2008 query

    i have 4000 records, and i need to pick randomize 500 records. i need that 500 records will insert to GoodTable and the rest 3500 records will insert to BadTable how to do it using sql-serv…
  • Help with SQL Server 2008 query

    I have this table: id name 1 A 1 B 2 C 2 D 2 E 3 F and I need to get this: id qty 1 2 2 2 3 1
  • SQL-Server-2008: XML Question

    I am wondering how to obtain the following from a query using xml on sql server 2008. I need: 1. The XML Version 2. The xmlns 3. And one parent node called "Brand" containing many child nodes …