F.Kahalaj F.Kahalaj - 1 month ago 10
SQL Question

datediff between 2 asymmetric column from same table

how can i use date difference between two asymmetric row from same table?

select
[endtime]
,[realtime]
from [prin]
where ([realtime] is not null) and ([endtime] is not null and)
order by [realtime]


between end date time and next start date time

Answer

Use the DateDiff function to get the difference between two dates. Add the day parameter to get the difference in days.

SELECT 
[endtime]
,[realtime]
,datediff (day,[endtime],lead ([realtime]) over (order by [realtime])) as days_endtime_2_next_realtime
FROM [prin]
WHERE ([realtime] is not null) 
AND ([endtime] is not null) 
ORDER BY [realtime] 
Comments