Monday, February 20, 2017

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




No comments:

Post a Comment