In this post, I want to take a look at datediff, how it works and how I use it as part of my job.
As per the Microsoft Documentation, “This function returns the count (as a signed integer value) of the specified datepart boundaries crossed between the specified startdate and enddate.”
The syntax for DATEDIFF looks like this
DATEDIFF ( datepart , startdate , enddate )
Let’s break that down,
So let’s say we want to find out how many days there are between the same date today 20 years ago and today, to do that we could write
SELECT DATEDIFF(dd,DATEADD(yy,-20,GETDATE()),GETDATE()) [Days]
This example is getting the start of the previous week by calculating the midnight of the current day then subtracting 6 days
SELECT DATEADD(day,-6,DATEADD(wk,DATEDIFF(wk,6,GETDATE()),6))
This part of the query
DATEADD(wk,DATEDIFF(wk,6,GETDATE()),6)
Gets the start of the current day, the start being defined as midnight
The second part then substracts 6 days from that date
DATEADD(day,-6,)
Giving us the start of the current week.