Skip to content
Advertisement

Get number of days between two dates in SQL

Fleet_ID State ODO_Reading Date_Added
1 F1 State 1 42000 10/01/2021
2 F1 State 1 43100 14/03/2021
3 F1 State 1 43900 26/04/2021
4 F2 State 1 63010 22/02/2021
5 F2 State 1 70800 06/04/2021
6 F3 State 2 17000 03/01/2021
7 F3 State 2 18560 19/03/2021
8 F3 State 2 19695 23/04/2021

Above is a sample database

Is it just a simple GET DATE command in order to get the number of days between two dates? I have tried to GET DATE but I must be doing it incorrectly.

I basically want to know the time passed in days between the dates in rows 1 & 2.

Advertisement

Answer

Use a combination of datediff and lead like this

datediff(Date_added,lead(Date_added,1))

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement