Javatpoint Logo
Javatpoint Logo

DATEDIFF() SQL Function

In Structured Query Language (SQL), DATEDIFF() is the function that is used to determine the difference between two dates or intervals. It is a powerful function provided by SQL to manage time and date.

DATEDIFF() SQL Function

This article will teach us about DATEDIFF() SQL function, its syntax, needs, and examples.

Syntax of DATEDIFF() function:

In the above syntax, 'interval' defines the unit of time for which you want to find the difference. The value of the interval can be day, month, year, minute, hour, second, week, etc. 'startDate' is the starting date or time. 'endDate' is the ending date or time.

List of interval values with their abbreviations:

Interval Abbreviations
Year year, yy, yyyy
Day day, dd, d
Month month, mm, m
Quarter quarter, qq, q
Week week, ww, wk
Nanosecond nanosecond, ns
Microsecond microsecond, us
Millisecond millisecond, ms
Second second, ss, s
Minute minute, mi
Hour hour, hh

Needs of DATEDIFF() function:

  • The duration between events: It is used to calculate the duration between two events.
  • Scheduling and planning: It is used to find out the time left to complete specific tasks.
  • Age calculation: It helps to know the age of people based on their date of birth and current date.
  • Academic duration: It helps to find the length of the academic course.
  • Inventory management: It helps to trace the age or track the shelf life of the products.
  • Expiry and renewal dates: It helps to find out the remaining time for expiry and renewal dates to manage warranty, license, etc.
  • Travel duration: Travel websites use this function to calculate trip duration.
  • Loan calculations: Banking systems use this function to find loan periods and interest rates on the basis of the loan start and end dates.
  • Subscription services: It helps in handling the subscription services by calculating the remaining subscription time for the users.
  • Project management: Project management tools help to find the task durations, project completion dates, etc.
  • Employee leave management: HR systems use this function to track employee leave periods.

Examples of the DATEDIFF() function in Microsoft SQL Server:

We will understand the DATEDIFF() function properly with the help of examples.

Note: We will use the syntax in the context of Microsoft SQL Server. The syntax may differ from database to database.

Example 1:

Let us consider a table called 'Orders' with fields like orderId, orderDate, and deliveryDate.

Orders Table:

orderId orderDate deliveryDate
1 2023-05-28 2023-06-28
2 2023-07-25 2023-08-21
3 2023-06-20 2023-08-29
4 2023-08-01 2023-08-30
5 2023-06-19 2023-07-26

1. We will consider the above 'Orders' table and calculate the number of days between the orderDate and deliveryDate.

We will use the given statement to find the number of days:

We will use the given statement to find the number of years:

orderId orderDate deliveryDate daysDifference
1 2023-05-28 2023-06-28 31
2 2023-07-25 2023-08-21 27
3 2023-06-20 2023-08-29 70
4 2023-08-01 2023-08-30 29
5 2023-06-19 2023-07-26 37

2. We will consider the above 'Orders' table and calculate the number of years between the orderDate and deliveryDate.


orderId orderDate deliveryDate yearsDifference
1 2023-05-28 2023-06-28 0
2 2023-07-25 2023-08-21 0
3 2023-06-20 2023-08-29 0
4 2023-08-01 2023-08-30 0
5 2023-06-19 2023-07-26 0

3. We will consider the above 'Orders' table and calculate the number of months between the orderDate and deliveryDate.

We will use the given statement to find the number of months:


orderId orderDate deliveryDate monthsDifference
1 2023-05-28 2023-06-28 1
2 2023-07-25 2023-08-21 1
3 2023-06-20 2023-08-29 2
4 2023-08-01 2023-08-30 0
5 2023-06-19 2023-07-26 1

Example 2:

Let us consider a table called 'Products' with fields like productId, productName, and releaseDate.

Products Table:

productId productName releaseDate
100 Refrigerator 2023-06-23
101 Television 2022-07-10
102 Washing Machine 2021-05-24
103 Coffee Maker 2020-04-20
104 Printer 2021-02-19

We will consider the above 'Products' table and calculate the number of months since releaseDate of the product.

We will use the given statement to find the number of months:

Let us consider a table called 'Employees' with fields like employeeId, Name, hireDate, and terminationDate.

productId productName releaseDate months_since_release
100 Refrigerator 2023-06-23 1
101 Television 2022-07-10 12
102 Washing Machine 2021-05-24 26
103 Coffee Maker 2020-04-20 39
104 Printer 2021-02-19 29

Example 3:

Employees Table:

employeeId Name hireDate terminationDate
200 Anshika 2020-05-21 2022-05-25
201 Shruti 2021-04-12 2022-05-20
202 Deepanshu 2021-08-18 2023-09-11
203 Milan 2022-05-27 2023-06-14
204 Harsh 2023-01-20 2023-07-19

We will consider the above 'Employees' table and calculate the number of years worked by the employee.

We will use the given statement to find the number of years:


employeeId Name hireDate terminationDate years_worked
200 Anshika 2020-05-21 2022-05-25 2
201 Shruti 2021-04-12 2022-05-20 1
202 Deepanshu 2021-08-18 2023-09-11 2
203 Milan 2022-05-27 2023-06-14 1
204 Harsh 2023-01-20 2023-07-19 0

Example 4:

Let us consider a table called 'Meetings' with fields like meetingId, startTime, and endTime.

Meetings Table:

meetingId startTime endTime
1 2023-07-18 09:00 2023-07-18 10:00
2 2023-07-18 11:00 2023-07-18 12:00
3 2023-07-19 10:00 2023-07-19 12:30
4 2023-07-20 11:00 2023-07-20 12:30
5 2023-07-20 05:00 2023-07-20 06:00

We will consider the above 'Meetings' table and calculate the duration of each meeting in minutes.

To find out how many minutes each meeting lasted, we will use the given statement:


meetingId startTime endTime meeting_duration_minutes
1 2023-07-18 09:00 2023-07-18 10:00 60
2 2023-07-18 11:00 2023-07-18 12:00 60
3 2023-07-19 10:00 2023-07-19 12:30 150
4 2023-07-20 11:00 2023-07-20 12:30 90
5 2023-07-20 05:00 2023-07-20 06:00 60

Example 5:

Let us consider a table called 'Persons' which has fields like personId, Name, and birthDate.

Meetings Table:

personId Name birthDate
1 Rahul 2000-06-14
2 Kajal 2005-01-20
3 Manish 2010-02-25
4 Raghav 1999-11-27
5 Nishant 1996-05-10

We will consider the above 'Persons' table and calculate the age of each person in years.

We will use the given statement to find the age of each person:


personId Name birthDate age_years
1 Rahul 2000-06-14 23
2 Kajal 2005-01-20 18
3 Manish 2010-02-25 13
4 Raghav 1999-11-27 24
5 Nishant 1996-05-10 27

Example 6:

Let us consider a table called 'Subscriptions' with fields like subscriptionId, startDate, and endDate.

Meetings Table:

subscriptionId startDate endDate
1 2022-11-13 2023-06-06
2 2022-12-10 2023-05-12
3 2023-04-05 2023-07-04
4 2023-05-02 2023-06-25
5 2023-03-18 2023-07-05

We will consider the above'Subscriptions' table and calculate the duration of each subscription in weeks.

We will use the given statement to find the duration of each subscription:


subscriptionId startDate endDate duration_weeks
1 2022-11-13 2023-06-06 29
2 2022-12-10 2023-05-12 22
3 2023-04-05 2023-07-04 13
4 2023-05-02 2023-06-25 8
5 2023-03-18 2023-07-05 16

Example 7:

Let us consider a table called 'Projects' with fields like projectId, startDate, and endDate.

Projects Table:

projectId startDate endDate
1 2023-01-10 2023-05-02
2 2023-03-09 2023-07-13
3 2023-04-16 2023-05-11
4 2023-02-11 2023-04-30
5 2023-06-02 2023-08-25

We will consider the above 'Projects' table and calculate the duration of each project in days.

We will use the given statement to find the duration of each project:


projectId startDate endDate project_duration_days
1 2023-01-10 2023-05-02 112
2 2023-03-09 2023-07-13 126
3 2023-04-16 2023-05-11 25
4 2023-02-11 2023-04-30 78
5 2023-06-02 2023-08-25 84

Example 8:

Let us consider a table called 'Logs', which has fields like logId, logTime, and previousLogTime.

Logs Table:

logId logTime previousLogTime
1 2023-07-21 10:30 2023-07-21 09:00
2 2023-07-21 09:30 2023-07-21 08:00
3 2023-07-21 11:30 2023-07-21 10:00
4 2023-07-21 05:30 2023-07-21 03:00
5 2023-07-21 07:00 2023-07-21 06:00

We will consider the above 'Logs' table and calculate the time difference in minutes between logTime, and previousLogTime.

We will use the given statement to find the time difference:


logId logTime previousLogTime time_diff_minutes
1 2023-07-21 10:30 2023-07-21 09:00 90
2 2023-07-21 09:30 2023-07-21 08:00 90
3 2023-07-21 11:30 2023-07-21 10:00 90
4 2023-07-21 05:30 2023-07-21 03:00 150
5 2023-07-21 07:00 2023-07-21 06:00 60

Example 9:

Let us consider a table called 'Users' with fields like userId, Name, and lastLoginDate.

Users Table:

userId Name lastLoginDate
1 Abhishek 2023-06-19
2 Vishal 2023-05-01
3 Avni 2023-07-02
4 Khushi 2023-06-10
5 Himanshu 2023-07-05

We will consider the above 'Users' table and calculate the number of days since each user's last login.

We will use the given statement to find the number of days:


userId Name lastLoginDate days_since_last_login
1 Abhishek 2023-06-19 34
2 Vishal 2023-05-01 83
3 Avni 2023-07-02 21
4 Khushi 2023-06-10 43
5 Himanshu 2023-07-05 18

Example 10:

Let us consider a table called 'Sales' with fields like saleId, saleDate, and previous_quarter_end_date.

Sales Table:

saleId saleDate previous_quarter_end_date
1 2023-05-01 2023-04-02
2 2023-06-09 2023-05-06
3 2023-07-18 2023-06-17
4 2023-06-18 2023-05-06
5 2023-07-16 2023-06-10

We will consider the above 'Sales' table and calculate the number of quarters between the saleDate, and previous_quarter_end_date.

We will use the given statement to find the number of quarters:


saleId saleDate previous_quarter_end_date quartersDifference
1 2023-05-01 2023-04-02 0
2 2023-06-09 2023-05-06 0
3 2023-07-18 2023-06-17 1
4 2023-06-18 2023-05-06 0
5 2023-07-16 2023-06-10 1

Conclusion:

In this article, we have learned about the DATEDIFF() SQL function, which is used to find the difference between two intervals. We have acquired knowledge about the needs of this function. We have seen various examples to understand the usage of the DATEDIFF() SQL function in real-world scenarios, such as finding the age of people, calculating the difference between two dates or times, and many more.


Next Topic#





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA