How to find the difference between two given dates in SQL Server?
788
22-Nov-2021
Updated on 20-Jun-2023
Aryan Kumar
20-Jun-2023To find the difference between two given dates in SQL Server, you can use the DATEDIFF() function. The DATEDIFF() function takes three arguments:
The DATEDIFF() function returns the difference between the two dates in the specified unit of time. For example, the following query will return the difference between the dates '2023-01-01' and '2023-01-15' in days:
SQL
This query will return the value 14, which means that there are 14 days between the two dates.
Here is the syntax for the DATEDIFF() function:
SQL
The DATEDIFF() function can also be used to find the difference between two datetimes. For example, the following query will return the difference between the datetimes '2023-01-01 12:00:00' and '2023-01-15 12:00:00' in hours:
SQL
This query will return the value 336, which means that there are 336 hours between the two datetimes.
Ashutosh Kumar Verma
22-Nov-2021DATEDIFF():
SQL DATEDIFF() function is used to find the difference between two given dates in term of year, month or day.
Example:
Find the different between two date in Year.
Output: 2
Find the different between two date in Month.
Output: 17
Find the different between two date in Day.
Output: 527