Calculate the Difference Between Two Dates in SQL Server

Write the following query in SQL Server. Execute the Query.

  1. SELECT DATEDIFF(day,'2015-06-01','2015-06-18'AS Difference    


The difference calculated is the number of days between the two dates. This is because we have passed day as a parameter to the function. There are other functions also that we can use to get the difference.

Let us consider one more example, this time we will calculate the difference in months.

Write the following query in SQL Server.
  1. SELECT DATEDIFF(mm,'2015-06-01','2015-09-18'AS Difference    


Here the difference is in months since we have passed mm as the first parameter. We can use the following parameters as well to calculate the difference.

 

Type Parameters
year yy,yyyy
quarter qq,q
month mm,m
days of year dy,y
day dd,d
week wk,ww
week day dw,w
hour hh
minute mi,n
second ss,s
microsecond ms
millisecond mcs
nanosecond ns