Wednesday, November 23, 2011

Calculate difference of Time only between two dates while discarding difference of dates in SQL Server/ extract time and calculate difference between two dates in SQL Server


DECLARE @DateTimeOne DATETIME
DECLARE @DateTimeTwo DATETIME

SET @DateTimeOne = '1900-01-01 10:41:09.250'
SET @DateTimeTwo = GETDATE()


--hour = hh
--minute = mi, n
--second = ss, s
--millisecond = ms


SELECT  DATEDIFF(mi,SUBSTRING(CONVERT(VARCHAR,@DateTimeOne, 121), 12, 12)
,SUBSTRING(CONVERT(VARCHAR,@DateTimeTwo, 121), 12, 12) ) MinuteDifferenceinDate