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.
Este exercício faz parte do curso
Functions for Manipulating Data in SQL Server
Exercício interativo prático
Experimente este exercício completando este código de exemplo.
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;