473,545 Members | 1,989 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Function for converting to minutes in decimal?

I have column of data that have elapsed time in minutes.
For example:
1:15
1:47
23:12

I like to be able to use sql server to do simple math functions. Does
anyone have a simple function/formula that will convert into a decimal
minutes form?

EG: 1:30 should equal 1.5 minutes

Thanking you.

Masa
Nov 20 '05 #1
5 16881
Once you have the Hours (whole number part of the value), Minutes, and
Seconds (if you are using Seconds as well), the formula is:

Value = Hours +( (Minutes + Seconds/60)/60)

If you are not using Seconds, then you would use:

Value = Hours + (Minutes/60)

Jody
"Masahiro Ito" <ma**@pleasespa mgoaway.it> wrote in message
news:Xn******** *************** *@216.196.105.1 30...
I have column of data that have elapsed time in minutes.
For example:
1:15
1:47
23:12

I like to be able to use sql server to do simple math functions. Does
anyone have a simple function/formula that will convert into a decimal
minutes form?

EG: 1:30 should equal 1.5 minutes

Thanking you.

Masa

Nov 20 '05 #2
"Jody Gelowitz" <jo**@mail.visu alstatement.com > wrote in news:
Once you have the Hours (whole number part of the value), Minutes, and
Seconds (if you are using Seconds as well), the formula is:
Value = Hours +( (Minutes + Seconds/60)/60)
If you are not using Seconds, then you would use:
Value = Hours + (Minutes/60)


Thank you Jody.

I am trying this formula, but have trouble with it.
my numbers come from a text file like: 1:15, 1:45, 10:37, 123:14. They
are always minutes, then seconds.

My code looks much more complicated than your formula, but I can't seem
to make your formula work?

Here is my code. I think I got it working, but it runs quite slow - I do
this to millions of numbers.
Public Function Time2Decimal(By Val s As String)
Dim i As Integer
Dim dbl As Double
Dim str() As String = s.Split(":")
If str(1) = "00" Then
dbl = CType(str(0), Double)
Else
Dim dbl2 As Double = CType(str(1), Double)
dbl2 = dbl2 / 60
str(1) = dbl2.ToString
Dim split() As String = str(1).Split(". ")
If split(1).Length > 2 Then
split(1) = split(1).Remove (2, split(1).Length - 2)
End If
Dim sFin As String = str(0) & "." & split(1)
dbl = CType(sFin, Double)
End If
Return dbl
End Function

Masa
Nov 20 '05 #3
"Masahiro Ito" <ma**@pleasespa mgoaway.it> wrote...
My code looks much more complicated than your formula, but I can't seem
to make your formula work?


This should work:

Private Function TimeToMinutes(B yVal s As String) As Double
Dim strs() As String = s.Split(":"c)
MinSeconds = CDbl(strs(0)) + (CDbl(strs(1)) / 60)
End Function

Test it with this:

Console.WriteLi ne(TimeToMinute s("1:15").ToStr ing)
Console.WriteLi ne(TimeToMinute s("2:45").ToStr ing)
Console.WriteLi ne(TimeToMinute s("30:37").ToSt ring)
Console.WriteLi ne(TimeToMinute s("123:14").ToS tring)

By the way... if it is possible that the string could be "0" or even "15"
with no ":" separator it is a good idea to avoid adding additional code in
the algorithm to handle it. Rather conform the data right away and use the
common routine. So for instance if you do need to check for zero or a
single value (you have to decide if a single value represents minutes or
seconds) then check for the separator and if there isn't one then just add
it. Pass this along to the converter. If you need to you can place those
lines of code in TimeToMinutes but they should appear first... not
interspersed inside.

Tom

Nov 20 '05 #4
"Tom Leylan" <ge*@iamtiredof spam.com> wrote...

Oh wouldn't you know... I changed the function name as I posted the code and
missed the return value...
Private Function TimeToMinutes(B yVal s As String) As Double
Dim strs() As String = s.Split(":"c)
MinSeconds = CDbl(strs(0)) + (CDbl(strs(1)) / 60)
End Function


Here it is again with the lines that will handle "0" or "15" (assuming any
value without the ":" separator is seconds. It is easy to make it assume
minutes if you need that instead.

Private Function TimeToMinutes(B yVal s As String) As Double

If s.IndexOf(":"c) < 0 Then
s = "0:" & s
End If

Dim strs() As String = s.Split(":"c)
Return Convert.ToDoubl e(strs(0)) + (Convert.ToDoub le(strs(1)) / 60)

End Function
Console.WriteLi ne(TimeToMinute s("0").ToString )
Console.WriteLi ne(TimeToMinute s("15").ToStrin g)

Console.WriteLi ne(TimeToMinute s("1:15").ToStr ing)
Console.WriteLi ne(TimeToMinute s("2:45").ToStr ing)
Console.WriteLi ne(TimeToMinute s("30:37").ToSt ring)
Console.WriteLi ne(TimeToMinute s("401:20").ToS tring)
Nov 20 '05 #5
"Tom Leylan" <ge*@iamtiredof spam.com> wrote in
Here it is again with the lines that will handle "0" or "15" (assuming
any value without the ":" separator is seconds. It is easy to make it
assume minutes if you need that instead.
Private Function TimeToMinutes(B yVal s As String) As Double
If s.IndexOf(":"c) < 0 Then
s = "0:" & s
End If
Dim strs() As String = s.Split(":"c)
Return Convert.ToDoubl e(strs(0)) + (Convert.ToDoub le(strs(1))
/ 60)


Very very thank you! perfect code - much better than mine.
Nov 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
12002
by: Steve - DND | last post by:
We're currently doing some tests to determine the performance of static vs non-static functions, and we're coming up with some odd(in our opinion) results. We used a very simple setup. One class had a static function, and the one class had a non-static function. Both of these functions did the exact same thing. The test function: public...
9
7702
by: Bill L. | last post by:
We recently noticed that the vb.net CStr function yields different results than the vb6 version when converting SQL decimal data. If, for example, the data is defined in SQL as decimal(19,10), the vb.net CStr function will return ten digits to the right of the decimal, regardless of the data value. In this case, if the data value is 5, the...
17
5637
by: nomenklatura | last post by:
Hi, System.Math.Round function is confused me. for example i want to round 3.245 in with decimal symbol Result should be = 3.25 When i try to this in vb: A = 3.245 X = Round(A, 2) then x=3.24 , result is is false
6
26445
by: Harlin Seritt | last post by:
I would like to take milliseconds and convert it to a more human-readable format like: 4 days 20 hours 10 minutes 35 seconds Is there something in the time module that can do this? I havent been able to find anything that would do it. Thanks,
6
7595
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards, Karthi
27
3101
by: Terry | last post by:
I am getting the following warning for the below function. I understand what it means but how do I handle a null reference? Then how do I pass the resulting value? Regards Warning 1 Function 'Dec2hms' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used.
2
7584
by: dwasbig9 | last post by:
Hi Group (fairly limited knowledge of Access and almost none of Access VBA. Using Access 2003). I need to sum time, I've found through the groups archive an sql extract that led me to this SELECT Format(Sum(Table2.time),"Short Time") AS SumOftime FROM Table2; Which works fine but the article also said I would need a Function to
4
3446
by: Jeff | last post by:
Hey ..NET 2.0 In my code am I trying to convert an int value into a decimal: decimal d = 0; int k = 87664; d = Convert.ToDecimal(k/100);
3
1466
by: HowHow | last post by:
I am using Excel 2000 to calculate the pay for worker and there are 4 possibility: 1.If actual work time is within 5 minutes under rostered time, pay rostered time 2.If actual work time is 5 minutes more under rostered time, pay actual work time 3.If actual work time is within 5 minutes over rostered time, pay actual work time 4.If actual work...
0
7420
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7680
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7446
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7778
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
4966
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3476
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3459
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1908
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
731
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.