You have two columns of the date type and you want to calculate the difference between them.
travel table, there are three columns:
arrival. You'd like to calculate the difference between
departure, or the number of days from
arrival to departure inclusively.
travel table looks like this:
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
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,
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.