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.
This exercise is part of the course
Functions for Manipulating Data in SQL Server
Hands-on interactive exercise
Have a go at this exercise by completing this sample 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;