Jeg har lavet denne her:
DECLARE @start datetime , @End Datetime
SET @start ='2007-05-26 21:45:40.287'
SELECT TOP 100 * FROM stamdata
SET @End = '2007-05-26 22:46:51.297'
select /*Cast(Left(CAST(DateDiff(ss, @start, @end)/60 as float)/60,
CharIndex('.', CAST(DateDiff(ss, @start, @end)/60 as float)/60)-1)
as varchar) + ':' +
Cast(Round(Cast(Substring(CAST(CAST(DateDiff(ss, @start, @end)/60
as float)/60 AS varchar),
CharIndex('.', CAST(DateDiff(ss, @start, @end)/60
as float)/60),4) as float)*60,0) as varchar) AS TimeHourMin,*/
@start DateTime1, @end DateTime2,
convert(varchar, @start, 114) AS Time1,
convert(varchar, @end, 114) AS Time2,
DateDiff(s, @end, @start) TimeDifferences,
CASE WHEN DateDiff(ms, @end, @start)<0 THEN '-' END,
RIGHT('0'+RTRIM(abs(DateDiff(hh, @end, @start))%60),2) AS Hour,
RIGHT('0'+RTRIM(abs(DateDiff(mi, @end, @start))%60),2) AS Min,
RIGHT('0'+RTRIM(abs(DateDiff(s, @end, @start))%60),2) AS Sek,
RIGHT('0'+RTRIM(abs(DateDiff(ms, @end, @start)%100)),2) AS milisek,
CASE WHEN DateDiff(ms, @end, @start)<0 THEN '-' END+
RIGHT('0'+RTRIM(abs(DateDiff(hh, @end, @start))%60),2)+':'+
RIGHT('0'+RTRIM(abs(DateDiff(mi, @end, @start))%60),2)+':'+
RIGHT('0'+RTRIM(abs(DateDiff(s, @end, @start))%60),2) AS TimeDiff
CASE WHEN DateDiff(ms, @end, @start)<0 THEN '-' END bruges til at bestemme om tallet er positivt eller negativt.
Muligvis skal den ændres til
CASE WHEN DateDiff(ss, @end, @start)<0 THEN '-' END
Hvis du kun vil bruge sekunder.
Den vigtige del er:
CASE WHEN DateDiff(ms, @end, @start)<0 THEN '-' END+
RIGHT('0'+RTRIM(abs(DateDiff(hh, @end, @start))%60),2)+':'+
RIGHT('0'+RTRIM(abs(DateDiff(mi, @end, @start))%60),2)+':'+
RIGHT('0'+RTRIM(abs(DateDiff(s, @end, @start))%60),2) AS TimeDiff
Du kan tilføje millisekunder hvis nødvendigt.