Hours and minutes calculations

J

Joe

I'm looking for the best way to calculate time in hours and minutes from two datetime fields, i.e., "04/01/2004 2300" and "04/02/2004 0635." I would like the results displayed on the form immediately, but not necessarily stored. Additionally, the code should lend itself to the calculation of total hours and minutes (not days, weeks or years) in a summary report. Any ideas as to what might be the optimum approach using T-SQL in a project file? Thanks for any ideas.
 
R

Ron Weiner

Joe;

Don't know if this is the best way, but it is a way! I'd use the Sql
DateDiff() function. Here is an example.

declare @startDT as datetime
declare @endDT as datetime

set @startDT = cast('04/01/2004 23:00' as datetime)
set @endDT = cast('04/02/2004 06:35' as datetime)

Select @startDT as StartDate,
@endDT as EndDate,
datediff(hh,@startDT,@endDT) as Hrs,
datediff(n,@startDT,@endDT) - (datediff(hh,@startDT,@endDT)*60) as mins

When run it returns:

StartDate End Date
Hrs mins
2004-04-01 23:00:00.000 2004-04-02 06:35:00.000 7 35

Ron W


Joe said:
I'm looking for the best way to calculate time in hours and minutes from
two datetime fields, i.e., "04/01/2004 2300" and "04/02/2004 0635." I would
like the results displayed on the form immediately, but not necessarily
stored. Additionally, the code should lend itself to the calculation of
total hours and minutes (not days, weeks or years) in a summary report. Any
ideas as to what might be the optimum approach using T-SQL in a project
file? Thanks for any ideas.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top