Last Updated : 23 Jul, 2025
The DATEDIFF()
function in SQL Server is a powerful tool used to calculate the difference between two dates or times. It returns an integer representing the number of date or time boundaries crossed between the specified dates, based on the specified date. This function is essential for tasks that involve comparing dates, calculating durations, or measuring intervals in SQL Server.
In this article, We will learn about the SQL Server DATEDIFF() Function in detail.
SQL Server DATEDIFF() FunctionDATEDIFF()
function in SQL Server is used to calculate the difference between two dates. Syntax:
SQL Server DATEDIFF function syntax is:
DATEDIFF(datepart, Start_date, End_date);Parameter
The DATEDIFF function accepts three parameters:
datepart
abbreviations
year
yyyy,yy,y
quarter
qq,q
dayofyear
dy
day
dd,d
week
ww,wk
weekday
dw,w
hour
hh
minute
mi,n
second
ss,s
millisecond
ms
Let's look at some examples of the DATEDIFF() function in SQL Server. Learning the DATEDIFF function with examples will help to understand the function better.
Example 1: DATEDIFF function on tableIn this example, we will use two columns and find difference in their dates using DATEDIFF function.
First, let's create a table and insert values.
Query:
CREATE TABLE Customer
(Start_date datetime2, End_date datetime2);
INSERT INTO Customer(Start_date, End_date)
VALUES ('2017-05-06 12:11:09', '2017-05-07 12:11:09');
Output:
Customer TableQuery:
SELECT DATEDIFF(day, Start_date, End_date)
FROM Customer;
Output:
1Example 2: Using DATEDIFF function to find difference between User-Defined DateTime
Using DATEDIFF() function and getting the difference between the two values of dates using variables which includes time as well, in second.
Query:
DECLARE @date1 VARCHAR(50);
DECLARE @date2 VARCHAR(50);
SET @date1 = '2019/2/1 09:55:44';
SET @date2 = '2020/12/12 07:45:22';
SELECT DATEDIFF(second, @Start_date, @End_date);
Output:
58744178Example 3: Find year difference between two dates using DATEDIFF function
Using DATEDIFF() function and getting the difference between two values of dates, in years.
Query:
SELECT DATEDIFF(year, '2010/01/12', '2021/01/12');
Output:
11Example 4: Selecting Constants from Date1 and Date2 with Negative Value
Using DATEDIFF() function and getting the negative difference between the two values of dates, in day.
Query:
SELECT DATEDIFF(day, '2021/2/1', '2010/12/12');
Output:
-3704Applications of DATEDIFF function in SQL Server
This function is used to find the difference between two specified values of date.
The DATEDIFF()
function in SQL Server is a versatile and straightforward tool for calculating the difference between two dates or times. It allows for flexible and precise measurement of intervals, making it invaluable for various data analysis and reporting tasks.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4