Perfect! When we wanted to find orders from March 2017, we used the following
WHERE O.OrderDate >= '20170301' AND O.OrderDate < '20170401'
There is another way to write this query. Take a look:
SUM(Amount) AS TotalRevenue
FROM Orders O
JOIN Customers C
ON O.CustomerId = C.CustomerId
WHERE O.OrderDate >= '20170301'
AND O.OrderDate < DATEADD(Month, 1, '20170301')
GROUP BY C.CustomerId,
This time, instead of providing the specific end date, we used the
DATEADD() function, which looks like this:
DATEADD(interval, number, date)
This function takes three arguments:
interval – the interval we want to add, such as
number – the amount of that interval to add.
date – the date to be modified.
In our example,
DATEADD(Month, 1, '20170301') means "add one month to March 1, 2017".