Time difference between two times in the same table

(Please note that I have seen a similar question on StackOverflow recently, however I can not find it anywhere – if anyone can find it please post the link)

I have a table that has a datetime field in it. For example:

  • how to pass multiple values in single parameter and how to convert varchar value into integers
  • How to find what foreign key references an index on table
  • Is there any way I can get .net stack traces in Sql Profiler, or a similar tool?
  • I am getting Status as “Not Started” in installing setup files step while installing SQL Server 2012 Advanced edition
  • When using GETDATE() in many places, is it better to use a variable?
  • SqlDependency subscription not dropped from dm_qn_subscriptions on shutdown
  • Table data

    DateTime date;
    int number;
    

    I need to find the difference between date in a row and date in the next row. I have tried with a query:

    select date as current_date, date - (select top 1 date where date > current_date from data) as time_difference from date
    

    however this won’t work, because of “current_date”. Is there a way I can do this with one query?

  • Create Data Audit in SQL Server
  • with(nolock) or (nolock) - Is there a difference?
  • restore SQL 2008 DB R2 to SQL2008 (no r2)
  • Generate Multiple and Filtered Drop + Create Stored Procedures
  • create Dynamic where in sql SP
  • Using Microsoft Query and ODBC to SQL Server, complicated query
  • One Solution collect form web for “Time difference between two times in the same table”

    SELECT  date AS current_date, next_date - date AS difference
    FROM    mytable mo
    CROSS APPLY
            (
            SELECT  TOP 1 date AS next_date
            FROM    mytable mi
            WHERE   mi.date > mo.date
            ORDER BY
                    date
            ) q
    
    MS SQL Server is a Microsoft SQL Database product, include sql server standard, sql server management studio, sql server express and so on.