Convert Float value to Hours and Minutes - sql

I have a float value, for example 0.999888, which I am getting from SQL database.
I have a variable in vbscript assigned to the float value from the SQL.
Lets say that I have this, for example
Dim TimeInFloat
TimeInFloat = 0.999888
I want to convert it to the hours and minutes either in SQL itself or VBscript.
Any suggestions?

You can convert it to hours and minutes in SQL by doing:
select floor(TimeInFloat * 24) as hours,
60 * (TimeInFloat * 24 - floor(TimeInFloat * 24)) as minutes
You can do similar logic in VBA.

In VBScript the CDate function allows you to convert float/double to time:
>>> WScript.Echo CDate(0.999888)
23:59:50

Related

SQL how i can convert from datetime to total hours?

I do this query and the result is a datetime, i try so much variants, but nothing work it... I want the result to be displayed in the number of total hours, like (in this case): 25:01:05 because i have 2 days en this datetime, I have had results like 01:01:05 which is when it only subtracts the hours from the datetime. I would like that as well as add the hours by the number of days, do it with the months if it can be
The time datatype in SQL Server only holds up to 24 hours.
I would recommend decimal hours instead:
select datediff(second, 0, calchstrabajos) / (60.0 * 60)
Note: I switched from millisecond to second because that is usually sufficient.
If you want this in the form of HH:MM:SS, then you would need to convert to a string. I don't recommend that.

sqlite converting milliseconds to HH:MM:SS.SSS

I have a column in the table that stores milliseconds as an Integer type in sqlite. The millisecond timestamp is stores the time it took for certain event happen. For example, it took 36 milliseconds for Action 1 occurred, the value 36 will be stored into the table.
I want to construct a SQL select statement that will format the millisecond timestamp to HH:MM:SS.SSS format, and so far I couldn't get it working.
Here's what the SQL query that I constructed.
select strftime('%H:%M:%f', table_foo.time/1000, 'unixepoch') as time from table_foo
The query result returned 00:00:00.000 where I was expecting to see 00:00:00.036 when the timestamp is 36 ms.
It appears that it is not showing the reminder properly.
Can someone points me what I did wrong? or what is the appropriate way to do what I need?
Thanks in advance.
try the following, replace int 1000 with decimal 1000.0.
select
strftime('%H:%M:%f', table_foo.time/1000.0, 'unixepoch') as time
from table_foo

Values vary after conversion from excel

I am trying to get values from one of the column from excel and i am facing strange issue that i cannot overcome so far.
Excel cells text we working on
Column format is set to: [h]:mm:ss so means hours could exceed 12/24.
When i am getting that values they are in double format as excel probbaly stores it in that way therefore i decided to write function to convert it back again to hours, minutes and seconds so i did that function:
Public Shared Function parseExcelHour(cellInput As String) As String
Dim excelHour As Double = 0
Dim hour As Integer
Dim min As Integer
Dim sec As Integer
Try
excelHour = [Double].Parse(cellInput)
Catch
End Try
sec = CInt((excelHour * 1440 * 60) Mod 60)
min = CInt((excelHour * 1440) Mod 60)
'mod (%) takes only the remainder as an int (if 5/4 = 1.25 , % only takes the number 1 that cannot be divided into an integer)
hour = CInt((excelHour * 1440) / 60)
' with the int cast you get only an integer.
Return hour & ":" & min & ":" & sec
End Function
However when i see the results, they are vary between excel and what i get after conversion. For three of them hours are either -1 or +1 if you compare. Also in one case we have additional + 1 minute. I suppose there is wrong hour calculation but i could be in wrong. See on screeshoot:
Results
Does anyone knows why i got those differences? Is that because i am missing something within my method or something else.
Excel stores a full Datetime equivalent as one double. The part before the decimal point is the days (since 1.1.1900; 1.1.1904 on Mac; note the bug that 1900 is faultily cosnider a leap year in Excel).
The part after is the time of the day, wich is what you apparently want:
https://stackoverflow.com/a/981865/3346583
What you are seeing in excel is meerely a ToString Foramting of the double value. Same way DateTime.ToString() would give you a string representation of whatever value is actually stored (most often a realy big unsigned int, with the ticks since a date).
A difference in full hours sounds like it might be a Timezone issue. But I am not aware that Excel stores Timezones in the first place (or what default timezone it asumes).

How to convert double to minutes in VB

I am currently storing a value as a double but will need to have the value converted to minutes for another function.
For example 1 would convert to 60 (minutes), 3,5 would equal to 210.
I tried timespan but I'm not getting what I am looking for.
TimeSpan.TotalMinutes should give you what you're looking for:
Dim minutes = TimeSpan.FromHours(hours).TotalMinutes

24hr time format string to sql time format

We have a column that stores a value in 24 hr time format. It is a string/text that users enter on to the interface. I need to convert it into sql time format so that I can do time difference calculations. How can I convert the string to time? Example:
StringColumn
1400
1600
needs to be
TimeColumn
1400
1600
so that I can calculate the time difference to get 2 hrs. Thanks.
If your string value are always 4 characters (meaning 01-09 and not 1-9 for early hours) then this works:
convert(time, stuff(StringColumn,3,0,':'))
You can do a conversion as in #jpw's answer, especially if you can use DATEDIFF on the results to get what you need.
Alternately you could perhaps do it as integer maths like:
SELECT (60*left('1900',2) + right('1900',2)) - (60*left('1400',2) + right('1400',2))
(I have used constants here, but you can replace '1900' and '1400' with column names).
CAST(LEFT(Stringcolumn, 2) + ':' + RIGHT(LEFT(Stringcolumn, 4), 2) AS TIME)