views:

64

answers:

1

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

+1  A: 

Use the DATEDIFF function e.g.

 select datediff(mi,Date1, IsNull(Date2, Date1)) from Table1

please read this from MSDN.

masoud ramezani
that does not produce the expected output
ScG