CommencerCommencer gratuitement

Arithmetic operations with dates

In this exercise, you will perform arithmetic operations on dates in two different ways: using the arithmetic operators ("+" and "-") and also using the DATEDIFF() function. You will use these variables:

DECLARE @date1 datetime = '2018-12-01';
DECLARE @date2 datetime = '2030-03-03';

Create a SELECT statement, in which to perform the following operations:

  • Subtract @date1 from @date2.
  • Add @date1 to @date2.
  • Using DATEDIFF(), calculate the difference in years between the results of the subtraction and the addition above.

What is the result returned by the DATEDIFF() function?

Cet exercice fait partie du cours

Functions for Manipulating Data in SQL Server

Afficher le cours

Exercice interactif pratique

Passez de la théorie à la pratique avec l’un de nos exercices interactifs

Commencer l’exercice