• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1076
  • Last Modified:

SQL subtract military time to get minutes

Hello experts,

I am trying to subtract two variables and come up with a duration in minutes:

declare @end_time char(4)
declare @begin_time char(4)
declare @duration int

set @end_time = 1400
set @begin_time = 1350

set @duration = DateDiff(mi, DATEADD(mi,@end_time,0), DATEADD(mi,@begin_time,0))

select @duration

Thoughts?

Thanks
0
robthomas09
Asked:
robthomas09
4 Solutions
 
tim_csCommented:
Are the times being fed in as 1400 instead of 14:00 or can this be changed?  If so then the following should work.  
declare @end_time smalldatetime
declare @begin_time smalldatetime
declare @duration int

set @end_time = '14:00'
set @begin_time = '13:50'

set @duration = DateDiff(mi, @end_time, @begin_time)

select @duration

Open in new window

0
 
dj_alikCommented:
@end_time  and  @begin_time  must be datetime
 and not char(4)
0
 
HainKurtSr. System AnalystCommented:
try this:

set @end_time = 1400
set @begin_time = 1350
set @diff int
set @diff = 60 * (@end_time - @begin_time) / 100

0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
HainKurtSr. System AnalystCommented:
what is 1350? is it 13:50 or 13.5? meaning 13:30?
0
 
HainKurtSr. System AnalystCommented:
this may work


declare @end_time int = 1450
declare @begin_time int = 1330
declare @duration int

set @duration =((@end_time / 100) - (@begin_time/100)) * 60 + (@end_time % 100) -(@begin_time % 100)

select @duration 

--> 80

Open in new window

0
 
robthomas09Author Commented:
Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now