Introduction:
Here I will explain datediff() function in SQL Server with example to get date difference in days hours minutes, years, month between two dates or calculate datediff in SQL Server in days hours minutes format.
Here I will explain datediff() function in SQL Server with example to get date difference in days hours minutes, years, month between two dates or calculate datediff in SQL Server in days hours minutes format.
Description:
In
previous articles I written SQL query to get data between two characters range, Get table size in SQL Server, SQL Server reset identity column to 1 in database, SQL Server acid properties and many articles related to SQL
Server. Now I will explain datediff() function in SQL
Server to get date difference in days hours minutes between two dates
Datediff() function
This
function is used to get time difference between two dates like days, hours,
minutes, seconds, years, months. Generally Datediff function will take 3
arguments.
Declaration of SubString function
DATEDIFF(DATEPART, startdate, enddate)
In this function startdate and enddate will be valid dates and Datepart will be one of the following like as shown below
Datepart
|
Datepart (Another
way declaration)
|
year
|
yy, yyyy
|
quarter
|
qq, q
|
month
|
mm, m
|
week
|
wk, ww
|
day
|
dd, d
|
hour
|
hh
|
minute
|
mi, n
|
second
|
ss, s
|
Examples of Datediff Function:
EX1:
SELECT DATEDIFF(hh,GETDATE()-1,GETDATE()) AS
DateDifference
|
Output:
|
In
above query we declared datepart as hh
that’s why that return hours difference between two dates
EX2:
SELECT DATEDIFF(day,'2013-02-07',GETDATE()) AS
DateDifference
|
Output:
|
In
above query we declared datepart as day
that’s why that return days difference between two dates
EX3:
SELECT DATEDIFF(ww,'2013-02-07',GETDATE()) AS DateDifference
|
Output:
|
In
above query we declared datepart as week
that’s why that return weeks difference between two dates
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
|
|||
6 comments :
Nice.. It works fine & more useful..
small but importaint article keep it brother
jkkj
gud
Gud But very gud
Nice.. It works fine
Note: Only a member of this blog may post a comment.