Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculating difference between two times 2

Status
Not open for further replies.

shaunhubbs

Technical User
Jun 6, 2005
57
CA
Hi all,

I have times stored as integers (Eg. 2351 = 11:51 PM, 955 = 9:55 AM) and I need to calclute the amount of minutes between them. Anyone have any suggestions?

TIA.

- Shaun
 
Oops my bad,
I inteded to ask you if 132 meant 13:02 or 1:32?
 
Kinda ugly but should work:
Code:
declare @start int,
@end int
set @start = 955
set @end = 2351
select datediff(mi, convert(datetime, case when len(@start) = 4 then left(@start, 2) + ':' + right(@start, 2)
                                           when len(@start) = 3 then left(@start, 1) + ':' + right(@start, 2)
	                                   else '00:' + convert(varchar, @start) end), 
                    convert(datetime, case when len(@end) = 4 then left(@end, 2) + ':' + right(@end, 2)
                                           when len(@end) = 3 then left(@end, 1) + ':' + right(@end, 2)
	                                   else '00:' + convert(varchar, @end) end))

PS: Will update you with an better one.
Regards,
AA
 
Works great! Thanks a lot AA.

I modified it slightly in case a time was just after midnight (meaning the length of the time would actually be 1 character). I will post here in case anyone runs into a similar problem:

SELECT @temp_length = datediff(mi,
convert(datetime, case when len(@proc_start) = 4 then left(@proc_start, 2) + ':' + right(@proc_start, 2)
when len(@proc_start) = 3 then left(@proc_start, 1) + ':' + right(@proc_start, 2)
when len(@proc_start) = 2 then '00:' + convert(varchar, @proc_start)
else '00:0' + convert(varchar, @proc_start)
end),
convert(datetime, case when len(@proc_end) = 4 then left(@proc_end, 2) + ':' + right(@proc_end, 2)
when len(@proc_end) = 3 then left(@proc_end, 1) + ':' + right(@proc_end, 2)
when len(@proc_end) = 2 then '00:' + convert(varchar, @proc_end)
else '00:0' + convert(varchar, @proc_end)
end))
 
Oh and previous to that posted SELECT statement I had to manipulate the numbers a little in case the procedure started before midnight and ended after:

IF @proc_end < @proc_start
BEGIN
SELECT @proc_end = @proc_end + 1200
SELECT @proc_start = @proc_start - 1200
END
 
I do not think you need another case for len = 2 since 2 or less is taken care of in the else stmt.

Regards,
AA
 
I needed the extra zero after the ':' so it actually did require that last statement otherwise if my time was '2' then it would put '00:2' which was more like 20 minutes after midnight than two minutes after midnight.

Thanks!
 
I do not think that is true.

check this out:
Code:
declare 
@start int
set    @start = 2
select convert(datetime, case when len(@start) = 4 then left(@start, 2) + ':' + right(@start, 2)
                              when len(@start) = 3 then left(@start, 1) + ':' + right(@start, 2)
	                          else '00:' + convert(varchar, @start) end)

Regards,
AA

 
Um... how about plain ol cryptic integer/modulo math? :)
Code:
select @end/100*60 + @end%100 - (@start/100*60 + @start%100)

------
"There's a man... He's bald and wears a short-sleeved shirt, and somehow he's very important to me. I think his name is Homer."
(Jack O'Neill, Stargate)
[banghead]
 
That is fantastically simple. Thanks a lot for that solution as well.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top