Calculate hours difference in datetime

Posted by ScG on Stack Overflow See other posts from Stack Overflow or by ScG
Published on 2010-04-18T06:03:32Z Indexed on 2010/04/18 6:13 UTC
Read the original article Hit count: 315

Filed under:

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

© Stack Overflow or respective owner

Related posts about sql-server