Instruction
Perfect! To count registrations in 2016, we use the following WHERE clause:
WHERE RegistrationDate >= '20160101' AND RegistrationDate < '20170101';
There is also an alternative way of writing this query. Take a look:
SELECT COUNT(CustomerID) AS RegistrationCount2016 FROM Customers WHERE RegistrationDate >= '20160101' AND RegistrationDate < DATEADD(year, 1, '20160101');
This time, instead of providing the end date specifically, we used the DATEADD(interval, number, date) function. It takes three arguments:
interval– the interval we want to add, such asyearormonth.number– the number of intervals to add.date– the date to which the interval will be added.
In our example, DATEADD(year, 1, '20160101') means "add one year to the date of 1 January 2016".
Exercise
Modify the template which contains the query from the previous exercise, so that it uses the DATEADD() function. (We want to see the first half of the year 2017.)
Stuck? Here's a hint!
Use the following invocation:
DATEADD(month, 6, '20170101')




