Home Forums SQL Server 2012 SQL Server 2012 - T-SQL DateDiff one Date column of First Row and the other Date column of second row. RE: DateDiff one Date column of First Row and the other Date column of second row.

  • Yep, as Sean said I assumed that you use SQL 2012 and windows offset functions are a good choice in situations where you want to refer to some other row from your row.

    Anyway, if you cannot use SQL 2012 the following query is logically equivalent to the query I provided in previous post:

    SELECT RowID,

    CASE WHEN DATEDIFF(DAY, Date2,

    (

    SELECT TOP 1 Date1

    FROM YourTable t2

    WHERE t1.IDNo = t2.IDNo AND t1.RowId < t2.RowId

    ORDER BY t2.RowId ASC

    )

    ) < 14

    THEN 1

    ELSE 0

    END IsDateDiffLess14_Calculation

    FROM YourTable t1

    ORDER BY RowID

    ___________________________
    Do Not Optimize for Exceptions!