How to Calculate the Difference Between Two Dates in T-SQL (2023)


You have two columns of the date type and you want to calculate the difference between them.


In the travel table, there are three columns: id, departure, and arrival. You'd like to calculate the difference between arrival and departure, or the number of days from arrival to departure inclusively.

The travel table looks like this:


How to Calculate the Difference Between Two Dates in T-SQL (1)


SELECT id, departure, arrival, DATEDIFF(day, departure, arrival) AS date_difference, DATEDIFF(day, departure, arrival) + 1 AS days_inclusiveFROM travel;

The result is:



To find the difference between dates, use the DATEDIFF(datepart, startdate, enddate) function. The datepart argument defines the part of the date/datetime in which you'd like to express the difference. Its value can be year, quarter, month, day, minute, etc. In this example, you want to express the difference in days, so the first argument is day. The two other arguments are the date from which you start counting and the date when you stop counting – In this case, departure and arrival, respectively.

In most cases, what you really want is the number of days from the first date to the second date inclusively. Then you need to add 1 day to the difference in days: DATEDIFF(day, departure, arrival) + 1.

Top Articles
Latest Posts
Article information

Author: Laurine Ryan

Last Updated: 11/12/2023

Views: 6153

Rating: 4.7 / 5 (57 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Laurine Ryan

Birthday: 1994-12-23

Address: Suite 751 871 Lissette Throughway, West Kittie, NH 41603

Phone: +2366831109631

Job: Sales Producer

Hobby: Creative writing, Motor sports, Do it yourself, Skateboarding, Coffee roasting, Calligraphy, Stand-up comedy

Introduction: My name is Laurine Ryan, I am a adorable, fair, graceful, spotless, gorgeous, homely, cooperative person who loves writing and wants to share my knowledge and understanding with you.