CommencerCommencer gratuitement

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.

Cet exercice fait partie du cours

Functions for Manipulating Data in SQL Server

Afficher le cours

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de code.

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;
Modifier et exécuter le code