I have a table where i need to convert for example 2.6 days or 5 days, 3.2 days etc to hours. Since it is above 24 hours format i know i need to convert to nvarchar. I searched many forums and i cannot see exactly how can i do specifically this, i think i have to convert this days to seconds and then from seconds to nvarchar format. does anyone have any idea?
Thx all :)
You seem to think you need to use nvarchar. But that is not necessarily the case. If you can store your hours as decimal. The solution is simple
hours = days * 24
If you want to break it down to hours minutes and seconds you need to convert it to seconds. There are 24 hours in a day, 60 minutes in an hour and 60 seconds in a minute.
seconds = days * 24 * 60 * 60
Once you have the seconds you can use the division operator (/) togheter with the modulo operator (%). If you store your days as decimal (3,1) you will not have to worry about seconds, because the resolution is too low.
select
days
,CAST(days * 24 * 60 * 60 as int) as totalseconds --days converted to full seconds
,CAST(days * 24 * 60 as int) as totalminutes --days converted to full minutes
,CAST(days * 24 as int) as totalfullhours --days converted to full hours
,CAST(days * 24 * 60 as int) % 60 as remainingminutes -- since the row above contains full hours, we need to find out how many minutes are in the split hours f ex 0.2 hours
,CAST(days * 24 * 60 * 60 as int) % 60 as remainingseconds -- this will always be 0 because of your precision
from YourTable
If you still want the result as a single string column (HH::mm) it will will be
select
CAST(CAST(days * 24 as int) as varchar(10)) + ':' + RIGHT('0'+CAST(CAST(days * 24 * 60 as int) % 60 as varchar(10)),2)
from YourTable
Related
I have a list of hours, showing like this
2.52 (meaning 2hours 52 minutes)
3
3.63
3.33
2.94
2.52
How can I convert this to # of minutes?
I have a list of hours, showing like this 2.52 (meaning 2hours 52
minutes)
If the scale part shows the minutes, we should directly add it directly to the total minutes:
select col, TRUNC(col) * 60 + (col % 1 * 100) minutes
from values (3.63),(2.94) tmp(col);
If they are the percentage of the hour (which makes more sense), then this can give you the result:
select col, TRUNC(col) * 60 + round(col % 1 * 60) minutes
from values (3.63),(2.94) tmp(col);
I dont understand the value of "2.94" - why is it 2 hours and 94 minutes and not 3.34, which would be the same amount of overall minutes?
Based on above assumption, you can use POSITION and SUBSTRING to split the string into the hour- and minute-part and then do the maths. First part of below query is extracting the left side of the dot and is multiplying it with 60, second part is just putting the minutes on top.
WITH hours AS (SELECT 3.63 as hour_value UNION SELECT 3.33 UNION SELECT 2.94)
select substr(hour_value, 0, position('.' in hour_value))*60 + substr(hour_value,position('.' in hour_value)+1) from hours;
In case the right side of the dot is NOT minutes, but the percentage of the whole hour, then you could just go with
select hour_value*60 from hours;
Using TIME_FROM_PARTS:
SELECT col, TIME_FROM_PARTS(FLOOR(col), col % 1 * 60, 0)
FROM tab;
Sample:
3.33
FLOOR(3.33) -> 3h
3.33 % 1 * 60 -> 0.33 * 60 -> 20 min
I am trying to add seconds to an existing date to find the end time. I have start time in one field and total duration in one field , i find in internet that we can add number to date but we should to dived on 86400
(chargestarttime + (NVL (callduration, 0) / 86400))
// where chargestarttime is date , callduration is number
// sample (chargestarttime is 2020-05-30 02:21:58 and callduration is 65 the output is 2020-05-30 02:23:03)
my question is why we need to div on 86400 ? if I convert the date to UNIX_TIMESTAMP and then add just the 65 it will give the same result without div on 86400 (24 hours) or there are a reason that we div on 86400 (24 hours)
why we need to div on 86400?
In date arithmetics, Oracle uses 1 to represent 1 day. If you start from a number of seconds, you need to convert that to days, so
chargestarttime + NVL(callduration, 0) / 60 / 60 / 24
-- seconds per minutes-----^
-- minutes per hour -----^
-- hours per day -----^
86400 is just the number of seconds there is in a day.
Your query is adding seconds as a fraction of a whole day (i.e. 1 day = 24 hours = 1440 minutes = 86400 seconds).
Alternatively, you could add seconds directly to the date using an INTERVAL data type:
chargestarttime + NVL(callduration, 0) * INTERVAL '1' SECOND
or
chargestarttime + NUMTODSINTERVAL( NVL(callduration, 0), 'SECOND' )
So i am putting the sql code in that i have shown below and my output that i get from the ProcessEnd minus ProcessStart is the duration time which comes out as "0 0:0:8.135". However, i need it to only show in terms of minutes, i don't want the hours or seconds, just the minutes the process runs.
TO_CHAR(rh.PROCESSSTART,'DD-MM-YYYY HH24:MI:SS') AS "PROCESSSTART",
TO_CHAR(rh.PROCESSEND,'DD-MM-YYYY HH24:MI:SS') AS "PROCESSEND",
(rh.PROCESSEND - rh.PROCESSSTART) AS "DURATION",
"0 0:0:8.135"
It looks that PROCESSEND and PROCESSSTART are DATEs.
If so, subtracting them results in number of days.
In order to get number of minutes, you'll have to multiply number of days by
24, as there are 24 hours in a day
60, as there are 60 minutes in an hour
so the final result would be
(rh.processend - rh.processstart) * 24 * 60 as number_of_minutes
In Excel or SQL, how can I convert the time string such as 2 hours 30 minutes into a decimal so that it reads 2.5 hours? This is in an excel spreadsheet so there are multiple values which also includes: 2 hours, 3 hours, 1 hour 15 minutes. Is there anyway to change all of the values using a formula or SQL query?
Keep it simple. Just do following Equation in Excel:
Minutes / 60 = Hours in Decimal
Example: 2 hours 30min
30 / 60 = 0.5 -> 2.5 hours
Example 2: 1 hour 15min
15 / 60 = 0.25 -> 1.25 hours
If you only use minutes: Example 3: 150min
150 / 60 = 2.5 hours
Its as easy as that :) Hope this helps.
From 2 hours 30 minutes you can take the hours and then divide the minutes by 60 like this:
Sub TestMe()
Dim myTime As String
myTime = "2 hours 30 minutes"
Debug.Print Round(Split(myTime)(0) + Split(myTime)(2) / 60, 2)
myTime = "2 hours 35 minutes"
Debug.Print Round(Split(myTime)(0) + Split(myTime)(2) / 60, 2)
End Sub
You can even make a custom function for this:
Public Function TimeToDouble(myTime As String) As Double
TimeToDouble = Round(Split(myTime)(0) + Split(myTime)(2) / 60, 2)
End Function
In excel, add three empty columns next to the time column. Then highlight the column and select "text to columns" select "space" as the deliminator. This will break the data up like this (without dots, they just help with format):
A B C D
2 hours . .
3 hours . .
1 hour 15 minutes
2 hours 30 minutes
each in their own column.
Then in a fifth column create the equation:
=A1+C1/60
Format the column to the number of decimals you want and you'll get the following data:
E
2.00
3.00
1.25
2.50
I'm not sure if your string is this predictable, but I tried a step-by-step script to put together a couple values. You can use a combination of LEFT(), RIGHT() to pry out the values you want to use for calculations, and CAST(col AS NUMERIC) to use the string values in division. Here is a sample that you can run in any session:
IF OBJECT_ID('TEMPDB..#TEMP') IS NOT NULL
DROP TABLE #TEMP
CREATE TABLE #TEMP (
[TimeString] NVARCHAR(20)
)
INSERT INTO #TEMP ([TimeString])
VALUES('2 hours 30 minutes')
,('20 hours 45 minutes')
,('3 hours')
SELECT [TimeString]
,LEFT([TimeString],2) [Left-side hours]
,RIGHT([TimeString],10) [Right-Side Minute]
,LEFT(RIGHT([TimeString],10),2) [Left of the right side Minute]
,CAST(LEFT(RIGHT([TimeString],10),2) AS NUMERIC(24,2)) [Numeric Minutes]
--Cast as NUMERIC is important for allowing the precision to calculate decimals.
,CAST(LEFT(RIGHT([TimeString],10),2) AS NUMERIC(24,2))/60 [Decimal Minutes]
,LEFT([TimeString],2)+CAST(LEFT(RIGHT([TimeString],10),2) AS NUMERIC(24,2))/60 [Put hours and decimal minutes together]
,CASE WHEN [TimeString] LIKE '%minutes%' THEN LEFT([TimeString],2)+CAST(LEFT(RIGHT([TimeString],10),2) AS NUMERIC(24,2))/60
ELSE LEFT([TimeString],2) END [Final Column]
FROM #TEMP
Output:
The Final Column accounts for the records where there are no 'minutes' in the string. Let me know if this works!
If I have a table with a column representing minutes, how can I convert those to hours and minutes?
For example, if I have in a cell "70" to be shown in the select "1h 10min", "60" to be shown "1h", etc.
I would suggest returning two derived fields in one query, the first one to get hours (through division), the second one to get minutes (through modulo operator). Try this:
select
minutes / 60 as h,
minutes % 60 as m
from
minutes
And here's a SQLFiddle.
EDIT
The following shows how the result could be formatted, too. I also updated the fiddle:
select
minutes / 60 as h,
minutes % 60 as m,
cast(minutes / 60 as text) || 'h ' || cast(minutes % 60 as text) || 'min' as formatted
from
minutes
Maybe you want to add some conditions to only show hours or minutes greater 0.