Aan de slagGa gratis aan de slag

Calculating the difference between dates

DATEDIFF() is one of the most commonly-known functions for manipulating dates. It is used for retrieving the number of time units between two dates. This function is useful for calculating, for example:

  • How many years have passed since a specific event.
  • The age of a person at a point in time.
  • How many minutes it takes to process an order in a restaurant.

In almost all business scenarios you can find an example for which using this function proves to be useful.

In this exercise, you will use DATEDIFF() to perform calculations with the dates stored in the voters table.

Deze oefening maakt deel uit van de cursus

Functions for Manipulating Data in SQL Server

Cursus bekijken

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

SELECT
	first_name,
	birthdate,
	first_vote_date,
    -- Select the diff between the 18th birthday and first vote
	___(___, ___(YEAR, 18, ___), ___) AS adult_years_until_vote
FROM voters;
Code bewerken en uitvoeren