Myself Ashutosh Kumar Verma from Varanasi. I am a Software Developer in MindStick Software pvt. ltd. Prayagraj. I have completed my MCA from VBSP. University, Jaunpur in 2021.
To find the difference between two given dates in SQL Server, you can use the DATEDIFF() function. The DATEDIFF() function takes three arguments:
The first argument is the datepart, which specifies the unit of time that you want to measure the difference in. For example, you can use day, month, year, hour, minute, or second.
The second argument is the start date.
The third argument is the end date.
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
SELECT DATEDIFF(day, '2023-01-01', '2023-01-15');
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
DATEDIFF ( datepart, start_date, end_date )
datepart is the datepart that you want to measure the difference in.
start_date is the start date.
end_date is the end date.
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:
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
To 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.
DATEDIFF():
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