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)
Related
I'm struggling with this.
I have a column in Snowflake called DURATION, it is VARCHAR type.
The values include basically number in days, hours, minutes, seconds. The value could include either just the number with one unit of time (day or hour or minute or second) such as 3 hours or 14 minutes or 3 seconds or it could include the combination of either all units of time or a few such as 1 day 3 hours 35 minutes or 1 hour 9 minutes or 45 minutes 1 second.
The value could also be blank or invalid such as text or it could be indicating day, hour or minute but without a number (see the last 3 rows in the table below).
I would greatly appreciate it if you guys could help me with the following:
in SNOWFLAKE, convert all valid values to number type and normalize them to minutes (e.g. the resulted value for 7 Hours and 13 Minutes would be 433).
Thanks a lot, guys!
DURATION
1 Second
10 Seconds
1 Minute
3 Minutes
20 Minutes
1 Hour
2 Hours
7 Hours 13 Minutes
1 Hour 1 Minute
1 Day
1 Day 1 Hour
1 Day 1 Hour 1 Minute
1 Day 10 Hours
2 Days 1 Hour
3 Days 9 Hours
1 Day 3 Hours 45 Minutes
Duration (invalid)
Days
Day Minute
Minutes
I tried many things using regex_substr, try_to_number, coalesce functions in CASE statements but I'm getting either 0s or NULL for all values. Very frustrating
I think you would want to use STRTOK_TO_ARRAY in a CTE subquery or put into a temp table. Then you could use ARRAY_POSITION to find the labels and the index one less than the label should be the value. Those values could be put into separate columns with a case for each label pulling the found values. The case statements could be computed columns if you insert the results of the first query into a table. From there you can concatenate colons and cast to a time type and use datediff, or do the arithmetic to calculate the minutes.
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 have a column 'Duration' it holds the time the therapist spent with the client.
This is always entered as minutes so if the time was 3 hours it is entered as 180. I would like to set this in the query as 3.
This is how it is reporting from a canned report: Total duration time is the entered column, it is
defined as int,null. I would like to make this calculation and formatiing, in the sql for the shown column 'total duration'.
total_duration_num total_duration
10 0:10
120 2:00
30 0:30
5 0:05
60 1:00
One means of achieving this is to use:
the floor function to round down when dividing the minutes by 60 (for whole hours)
the mod function to get the remaining number of minutes after putting however many can fit into "whole" 60-minute hours
the lpad function to put a leading zero before that number of minutes, if <10, so that you see :05 rather than :5 for example
The query would look like this:
select duration,
concat( floor(duration/60) , ':' , lpad(mod(duration,60),2,'0') ) as hrs_mins
from duration_table;
This is a demonstration:
http://sqlfiddle.com/#!9/a52b6c/1/0
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!
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)