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.

  • Here is a piece of code which calculates if the flag IsDateDiffLess14_Calculation should be set or not.

    SELECT RowID,

    CASE WHEN DATEDIFF(DAY, Date2, LEAD(Date1) OVER(PARTITION BY IDNo ORDER BY RowId)) < 14

    THEN 1

    ELSE 0

    END IsDateDiffLess14_Calculation

    FROM YourTable

    ORDER BY RowID

    I am not sure if it's required to have this flag as persistent column in the table. If it is you should modify the script and use it whenever a new row is added to the table to update the previous row only and not the whole table.

    ___________________________
    Do Not Optimize for Exceptions!