

If only a date value is assigned to a variable of a time or date data type, DATEDIFF sets the value of the missing time part to the default value: 00:00:00. If only a time value is assigned to a date data type variable, DATEDIFF sets the value of the missing date part to the default value. If startdate and enddate are both assigned only a time value, and the datepart is not a time datepart, DATEDIFF returns 0.ĭATEDIFF uses the time zone offset component of startdate or enddate to calculate the return value.īecause smalldatetime is accurate only to the minute, seconds and milliseconds are always set to 0 in the return value when startdate or enddate have a smalldatetime value. For second, the maximum difference is 68 years, 19 days, 3 hours, 14 minutes and 7 seconds. For millisecond, the maximum difference between startdate and enddate is 24 days, 20 hours, 31 minutes and 23.647 seconds. This case means that if we start at startdate '', and then count -2 days, we reach the enddate of ''.įor a return value out of range for int (-2,147,483,648 to +2,147,483,647), DATEDIFF returns an error. The int difference between the startdate and enddate, expressed in the boundary set by datepart.įor example, SELECT DATEDIFF(day, '', '') returns -2, hinting that 2036 must be a leap year. See Configure the two digit year cutoff Server Configuration Option for information about two-digit year values.
MYSQL DATE DIFF HOW TO
In this tutorial, you have learned how to use the SQL DATEDIFF() function to calculate the difference between two dates.Each specific datepart name and abbreviations for that datepart name will return the same value.Īn expression that can resolve to one of the following values: See the following example: SELECT DATEDIFF( '', '') It ignores all the time part of the date in the calculation. MySQL only returns the difference between two dates in days. Unlike SQL Server, MySQL has a slightly different DATEDIFF() function syntax: DATEDIFF(startdate,enddate) The following example shows how to use the DATEDIFF() function to calculate the year of services of employees up to January 1st, 2018: SELECT first_name,ĭATEDIFF( year, hire_date, '') year_of_servicesĬode language: SQL (Structured Query Language) ( sql ) DATEDIFF in MySQL In this case, it truncated the minute part and only consider the hour part. It also returns two because the DATEDIFF() function returns an integer only.

The following example illustrates how to use the DATEDIFF() function to calculate the difference in hours between two DATETIME values: SELECT DATEDIFF( hour, ' 01:00:00', ' 03:00:00') Ĭonsider the following example: SELECT DATEDIFF( hour, ' 01:00:00', ' 03:45:00') As shown clearly in the result, because 2016 is the leap year, the difference in days between two dates is 2×365 + 366 = 1096. Notice that the DATEDIFF() function takes the leap year into account. To get the number of month or day, you change the first argument to month or day as shown below: SELECT DATEDIFF( month, '', ''), The following example returns the number of year between two dates: SELECT DATEDIFF( year, '', '') Ĭode language: SQL (Structured Query Language) ( sql ) The DATEDIFF() function returns an integer value with the unit specified by the datepart argument. The startdate and enddate are date literals or expressions from which you want to find the difference. The following table illustrates the valid parts of date in SQL Server: Valid Date Part

The datepart is a part of the date in which you want the function to return. The following illustrates the syntax of the DATEDIFF() function in SQL Server: DATEDIFF ( datepart, startdate, enddate )Ĭode language: SQL (Structured Query Language) ( sql ) Arguments datepart To calculate the difference between two dates, you use the DATEDIFF()function. Summary: in this tutorial, you will learn how to use the SQL DATEDIFF() function to calculate the difference between two dates.
