Sql server: Get difference in Day, Hour, Minute and second between two dates - Free Asp.Net,MVC,AngularJs,Jquery,Javascript,Sql Server,WCF,Entity framework snippets and tutorial


Sql server: Get difference in Day, Hour, Minute and second between two dates

In this article I am going to explain how to find difference between two dates in day, hour, minute and second using sql server.

Implementation:
I want to get difference of two dates in days, hour, minutes and second. To get difference I have use datediff function of sql.

Here is the query that works for me:

DECLARE @startdate datetime , @enddate datetime
SET @startdate = '2017-01-01 9:28:30'
SET @enddate = GETDATE()
SELECT [Days]= DateDiff(DD, @startdate, @enddate)
  ,[Hours]= DateDiff(HH, @startdate, @enddate) % 24
  ,[Mins]= DateDiff(MI, @startdate, @enddate) % 60
  ,[Sec] =DateDiff(SS, @startdate, @enddate) % 60

 Output:

Sql server: Get difference in Day, Hour, Minute and second between two dates




What do you think about this article?

If you found this article useful, please share and follow on Facebook, Twitter, Google Plus and other social media websites. To get free updates subscribe to newsletter. Please put your thoughts and feedback in comments section.

Share this

Share on FacebookTweet on TwitterPlus on Google+
loading...


EmoticonEmoticon