Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

SQL Sever Datediff() Function to Show Date Difference in Days Hours Minutes

Feb 6, 2014
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.
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.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email
© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.