Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Customer registration
Customer cohorts
Summary

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:

  1. interval – the interval we want to add, such as year or month.
  2. number – the number of intervals to add.
  3. 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')