Skip to content

Instantly share code, notes, and snippets.

@tcartwright
Last active March 30, 2026 14:48
Show Gist options
  • Select an option

  • Save tcartwright/0d63a50f3d279f6b99703981cfe37090 to your computer and use it in GitHub Desktop.

Select an option

Save tcartwright/0d63a50f3d279f6b99703981cfe37090 to your computer and use it in GitHub Desktop.
SQL SERVER: Per Row Time diff with days and milliseconds in timespan format
-- expects rows to be in reverse order, if not in reverse order, then need to switch the columns in the datediff
CONCAT(
DATEDIFF(MILLISECOND, LEAD([all_logs].[timestamp]) OVER (ORDER BY [all_logs].[Id] DESC), [all_logs].[timestamp]) / 86400000, '.',
CONVERT(VARCHAR, DATEADD(MILLISECOND, DATEDIFF(MILLISECOND, LEAD([all_logs].[timestamp]) OVER (ORDER BY [all_logs].[Id] DESC), [all_logs].[timestamp]) % 86400000, 0), 114)
) AS [TimeDiff]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment