Reputation: 1073
I have a series of datetime values. I want to select records with a difference of 2 or more hours between them.
2010-02-11 08:55:00.000
2010-02-11 10:45:00.000
2010-02-11 10:55:00.000
2010-02-11 12:55:00.000
2010-02-11 14:52:00.000
2010-02-11 16:55:00.000
2010-02-11 17:55:00.000
2010-02-11 23:55:00.000
2010-02-12 00:55:00.000
2010-02-12 02:55:00.000
Expected (The next date compared is with the last date that qualified for the 2 hr difference):
2010-02-11 08:55:00.000
2010-02-11 10:55:00.000
2010-02-11 12:55:00.000
2010-02-11 16:55:00.000
2010-02-11 23:55:00.000
2010-02-12 02:55:00.000
I am using SQL 2005 or 2008
Upvotes: 1
Views: 2085
Reputation: 22920
Use the DATEDIFF function e.g.
select datediff(mi,Date1, IsNull(Date2, Date1)) from Table1
please read this from MSDN.
Upvotes: 1