How can I change hour to minutes in snowflake - sql

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

Related

How to convert Interval type to total hours in Bigquery?

Research is showing me to use Extract(epoch from interval), but I don't think that's an available function in BQ. Any thoughts?
I have an interval like so:
0-0 103 16:10:25
That output would be 0 years (in hours) + 0 months (in hours) + 103 days (in hours) + 16 + 10 min (in hours) + 25 seconds (in hours).
Consider below example
create temp function interval_to_hous(value string) as ((
select round(sum(cast(part as int64) * weight), 2)
from unnest(regexp_extract_all(value, r'\d+')) part with offset
join unnest([365*24, 365/12*24, 24, 1, 1/60, 1/3600]) weight with offset
using (offset)
));
with your_table as (
select '0-0 103 16:10:25' as col
)
select col, interval_to_hous(col) as hours,
from your_table
with output
Obviously, treating hours for year and month parts is approximate :o)
Hope, the rest of code is simple and self-explanatory

Convert days to hours in HHH:MM:SS Format (Nvarchar)

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

How do I convert a time string (2 hours 30 minutes) into a decimal so that it becomes 2.5?

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!

How can I convert and round-up seconds to the next full minute?

I have a time usage amount in an Oracle table stored as a number of seconds:
5
10
100
500
How can I convert the number of seconds to the number of minutes, rounded up to the next closest minute? For the seconds values above I want to get:
1
1
2
9
Assuming you are actually using Oracle, you can use the CEIL function to 'round up' the fractional number of minutes you get by dividing by 60 to the next integer.
ceil(your_column/60)
So with some sample values:
select seconds, ceil(seconds/60) as minutes
from your_table
order by seconds;
SECONDS MINUTES
---------- ----------
5 1
10 1
100 2
500 9
1800 30
You're trying to divide it by 60 and "trunc up". Unfortunately there isn't a built-in function that does exactly that, but trunc can still be used:
SELECT CASE TRUNC(myseconds/60) WHEN myseconds/60 THEN myseconds/60
ELSE TRUNC(myseconds/60) + 1
END
FROM my_table
EDIT:
Or in a more elegant form with a subquery:
SELECT CASE TRUNC(myminutes) WHEN myminutes THEN myminutes
ELSE TRUNC(myminutes) + 1
END
FROM (SELECT myseconds/60 AS myminutes
FROM myable)

Case statement for calculating hours

I have the following formula to convert the duration into hours. I want to do case where any duration which is 15 min or more Is considered as an hour. for instance 1 hour 15 min will be calculated as 2 hrs, 2 hrs 15 min will be calculated as 3 hrs and so on. if it is less than 15 min after an hour than it will be the hour. eg 1 hr 5 min will be considered 1 hour.
((Case T0.DurType when 'M' then (T0.Duration/100)/.60 when 'D' then (T0.Duration*8) Else T0.Duration End)) as 'Duration'
Are you doing this in SQL or as a formula field in Crystal?
Is case syntax required for some reason, or is that simply the approach you initially chose?
What unit of time does each increment of Duration represent, 1 second, 1 minute?
Assuming the following:
this is in sql
case syntax is not required
each increment of Duration is 1 minute
Then here is your correct formula, using ceiling rather than case:
ceiling(T0.Duration/60) as "Duration"
That will increment any partial decimal value to the next highest integer, e.g., 75 minutes / 60 = 1.25 hours, and ceiling will increment to 2. 180 minutes / 60 = 3.00 hours and ceiling will output 3.
EDIT:
I'm not sure what you mean by achieving it in sql & crystal... if you calculate it in sql, it's passed to Crystal and won't need any further transformation. Either way, here's both solutions:
Crystal: Assumes minutes are used. the "\" operator is integer division, so the decimal is dropped. A simple if/then/else iif is used to add either 1 or zero if the remainder minutes are 15 or more:
MINUTES \ 60 + IIF (MINUTES mod 60 >= 15,1 , 0)
In SQL (MySQL syntax, MSSQL/TSQL may vary) achieves the same as follows:
floor(MINUTES / 60) + IF( (MINUTES % 60) >= 15,1 , 0)