Saturday, July 28, 2012

How to Find day,week,year,month difference between two Dates in sql?

Introduction
             Here I am going to Explain you about how to Find the Difference of day,week,year,month between two dates in sql.

Lets create one table called OrderDetails
Now Lets How Find the difference  Of days,week,year,month with the current date.

select DATEDIFF(day,OrderDate,GETDATE()) As Numberofdays,DATEDIFF(week,OrderDate,GETDATE()) AS NumberofWeek,
DATEDIFF(Month,OrderDate,GETDATE()) As NumberofWeek,DATEDIFF(year,OrderDate,GETDATE()) As NumberofYear
 from OrderDetails


Here GetDate() will return the current Date.
First parameter of DATEDIFF() define what you want to find day,week,month or year.
and second and third parameter define to which you have to find the difference.

Now out put of the above query will be.


Same you can also Find the diffrence of Hour, minute,second and  millisecond .just you have to place this into first Parameter of DATEDIFF().

0 comments:

Post a Comment