Sum of Time Difference

R

RN1

This is how I am calculating the time difference:

------------------------
Function TimeDiff(ByVal StartDateTime, ByVal EndDateTime)
Dim h As Integer
Dim m As Integer
Dim t1 As DateTime
Dim t2 As DateTime
Dim ts As TimeSpan

t1 = DateTime.Parse(StartDateTime)
t2 = DateTime.Parse(EndDateTime)

ts = t2 - t1
h = ts.Hours + (ts.Days * 24)
m = ts.Minutes

Return System.Math.Abs(h) & ":" & System.Math.Abs(m)
End Function
------------------------

For e.g. if StartDateTime is 05/08/2008 7:00:00 AM & EndDateTime is
05/08/2008 11:30:00 PM, then the above function will return 16:30 i.e.
16 hours & 30 minutes.

The result I am getting from the above function - I am inserting that
in a SQL Server DB table in a column named Duration whose datatype is
varchar. Now I want to add all the records under the Duration column &
get the total no. of hours & minutes.

How do I do it?
 
J

Joe Fawcett

RN1 said:
This is how I am calculating the time difference:

------------------------
Function TimeDiff(ByVal StartDateTime, ByVal EndDateTime)
Dim h As Integer
Dim m As Integer
Dim t1 As DateTime
Dim t2 As DateTime
Dim ts As TimeSpan

t1 = DateTime.Parse(StartDateTime)
t2 = DateTime.Parse(EndDateTime)

ts = t2 - t1
h = ts.Hours + (ts.Days * 24)
m = ts.Minutes

Return System.Math.Abs(h) & ":" & System.Math.Abs(m)
End Function
------------------------

For e.g. if StartDateTime is 05/08/2008 7:00:00 AM & EndDateTime is
05/08/2008 11:30:00 PM, then the above function will return 16:30 i.e.
16 hours & 30 minutes.

The result I am getting from the above function - I am inserting that
in a SQL Server DB table in a column named Duration whose datatype is
varchar. Now I want to add all the records under the Duration column &
get the total no. of hours & minutes.

How do I do it?
There maybe better ways, try one of the microsoft.public.sqlserver.* groups
but here's one way.
Use a combination of CHARINDEX and SUBSTRING to find the hours component,
cast to an INT and SUM. Use a similar technique for the minutes, or the
RIGHT function if there's always two digits.
Once you have these values you'll have to use the modulo (%) operator on the
minutes SUM by 60 to convert the excess to hours.
 
H

Hans Kesting

After serious thinking RN1 wrote :
This is how I am calculating the time difference:

------------------------
Function TimeDiff(ByVal StartDateTime, ByVal EndDateTime)
Dim h As Integer
Dim m As Integer
Dim t1 As DateTime
Dim t2 As DateTime
Dim ts As TimeSpan

t1 = DateTime.Parse(StartDateTime)
t2 = DateTime.Parse(EndDateTime)

ts = t2 - t1
h = ts.Hours + (ts.Days * 24)
m = ts.Minutes

Return System.Math.Abs(h) & ":" & System.Math.Abs(m)
End Function
------------------------

For e.g. if StartDateTime is 05/08/2008 7:00:00 AM & EndDateTime is
05/08/2008 11:30:00 PM, then the above function will return 16:30 i.e.
16 hours & 30 minutes.

The result I am getting from the above function - I am inserting that
in a SQL Server DB table in a column named Duration whose datatype is
varchar. Now I want to add all the records under the Duration column &
get the total no. of hours & minutes.

How do I do it?

The best way would be not to store it as a string, but as a "float".
Store the value of the TotalHours property of the TimeSpan returned by
your method. Summing that column would then be easy.

Hans Kesting
 

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

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,078
Latest member
MakersCBDBlood

Latest Threads

Top