When you’re working with T-SQL, it’s common to need to work out how far apart two dates and or times are. Look at the query below:
In this case I'm asking how many days is it from 28th of February 2019 to 31st of July 2019? And if look at the answer:
We can see that it’s 153 days.
Now I used days here but now we could have used any of those other intervals that I discussed when we looked at DATEADD. So I could work at how many minutes that was or how many seconds that was between the two times. I could even say how many months?
The order of the parameters that you pass to this function are important. The first one is considered the starting date and/or time, and the second is the ending date and/or time. So if I had put those around the other way, I would have seen -153 instead of 153.
It's worth your while becoming proficient in SQL. If you'd like to learn a lot about T-SQL in a hurry, our Writing T-SQL Queries for SQL Server course is online, on-demand, and low cost.