How can I convert a time interval in date format (i.e. "2275 days") into number of years?
When I divide it by 365 it becomes something like ("6 days 5 hours 32 minutes 44 seconds"). whereas I want to return "6.23 year" or just "6.23".
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.
Do you know how to convert this duration number 145351401 to days, hours, minutes and seconds in SQL?
I tried
SELECT CAST(STUFF(STUFF(STUFF(cast(145351401 as varchar),3,0,':'),6,0,':'),9,0,'.') AS TIME)
but it gave me the hours, minutes and seconds but no days.
The above number is the difference between the below two dates, so it should have something like 7 days and the hours, minutes and seconds in the duration.
2022-09-08 16:21:48.400
2022-09-16 07:22:31.400
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
I am creating a query that shows me the time elapsed between two dates, only taking into account only the one that is Monday through Friday from 08:00 to 17:00, for example:
For example, if a petition opens on day 1 at 6:30 p.m. and closes on day 2 at 8:45 p.m., the TMO is 45 minutes.
If it closes on day 3 at 8:45, the TMO is 9 hours and 45 minutes.
Example 2:
If a petition opens on Friday at 16:45 and closes on Tuesday at 8:30, the MTO would be: 15 minutes on Friday, nine hours on Monday and 30 minutes on Tuesday for an MTO = 9 hours 45 minutes
The query is performed on a single column of type date as I show below
I currently use a LAG function to make the query, but I can not create something functional, not even optimal to incorporate, I would greatly appreciate your help.
In the solution below I will ignore the "lag" part of your problem, which you said you know how to use. I am only showing how to count "working hours" between any two date_times (they may be during or before or after work hours, and/or they can be on weekend days; the computation is the same in all cases).
Explaining the answer in words: For two given date-times, "start" and "end", calculate how many "work" hours elapsed from the beginning of the week (from Monday 00:00:00) till each of them. This is in fact a calculation for ONE date, not for TWO dates. Then: given "start" and "end", calculate this number of hours for each of them; subtract the "end" number of hours from the "start" number of hours. To the result, add x times 5 times 9, where x is the difference in weeks between Monday 00:00:00 of the two dates. (If they are in the same week, the difference will be 0.)
To truncate a date to the beginning of the day, we use TRUNC(dt). To truncate to the beginning of Monday, TRUNC(dt, 'iw').
To compute how many "work" hours are from the beginning of the date dt until the actual time-of-day we can use the calculation
greatest(0, least(17/24, dt - trunc(dt)) - 8/24)
(the results will be in days; we calculate everything in days and then we can convert to hours). However, in the final formula we must check to see if the date is a Saturday or Sunday, in which case this should just be zero. Or, better, we can adjust the calculation a bit later, when we count from the beginning of Monday (we can use least( 5*9/24, ...)).
Putting everything together:
with
inputs ( dt1, dt2 ) as (
select to_date('2017-09-25 11:30:00', 'yyyy-mm-dd hh24:mi:ss'),
to_date('2017-10-01 22:45:00', 'yyyy-mm-dd hh24:mi:ss')
from dual
)
-- End of SIMULATED input dates (for testing only).
select 24 *
( least(5 * (17 - 8) / 24, greatest(0, least(17/24, dt2 - trunc(dt2)) - 8/24)
+ (17 - 8) / 24 * (trunc(dt2) - trunc(dt2, 'iw')))
-
least(5 * (17 - 8) / 24, greatest(0, least(17/24, dt1 - trunc(dt1)) - 8/24)
+ (17 - 8) / 24 * (trunc(dt1) - trunc(dt1, 'iw')))
+ 5 * (17 - 8) / 24 * (trunc(dt2, 'iw') - trunc(dt1, 'iw')) / 7
)
as duration_in_hours
from inputs
;
DURATION_IN_HOURS
-----------------
41.500
I have a query to retrieve a set of non null records from a column x consisting of DATE format.
If count(x) = 35 then i need to display the value as 1 Month & 5 days
If 369 days then 1 year & 4 days or If 400 days then 1 year 1 month 5 days respectively
Query: In the above instance,unfortunately i am neglecting 0.25 days but How to tweak my actual requirement in such a way that i don't end up neglecting days and handle leap year logic too
How to solve this issue?
it is not clear if you need of a time's generic computing in years, months and days, based on averages of number of days of a month, etc.. or if you want an exact compute of the number of the months that pass starting froma adate.. for example if your total sum of 400 days start from 15 march, then the month is counted by 1 after, let's say, 15 days, and so the remaining days are 20.. I don't know if I explained..
In the first hypotesis, you may use the following pseudo-coded solution, that is a very approximative method..
however, if you know a start date, it is possible to compute exactly how many "bissextile" days are comprehended between your interval of days starting from your start date
let's say to have output variables Years, Months, Days .. and an input totalDaysdays assigned with your data retrieved from the db, then:
(pseudocode)
Years = trunc((totalDays / 365)
bissextileDays = trunc((totalDays / 365) / 4)
numDaysOffset = (totalDays Mod 365) + bissextileDays
Months = trunc(numDaysOffset / 30)
Days = numDaysOffset Mod 30
Actually i found something that will suit my requirement.
https://community.oracle.com/thread/2587161?start=0&tstart=0
select days,
floor(days / 365.25) years,
floor(mod(days,365.25) / (365.25 / 12)) months,
round(mod(days,365.25 / 12)) days
from periods
So this can produce expected output when number is given. This produces output as years,months and remaining days