How to convert Minutes to Hours Minutes and Seconds in SQL? - sql

Currently I am using this query, which gives me duration in minutes.
Part of My query
Select
E.xyme
From
(SELECT Timee as xyme from
(select round(avg(tableT.DURATIONMIN),2) as Timee
FROM ownerName.tableT tableT
where tableT.FLAG = 0 )
)E
Output would be
Xyme
----
125.58
Output Looking for
Xyme
----
2 hours, 5 minutes, 35 seconds
Part solution
I know we can use something like below but I am not able to implement.
floor((sysdate-Timee)*24)
|| ' HOURS ' ||
mod(floor((sysdate-Timee)*24*60),60)
|| ' MINUTES ' ||
mod(floor((sysdate-Timee)*24*60*60),60)
|| ' SECS ' Duration

You can convert the numeric value for the minutes to an interval type using the numtodsinterval() function, and then extract() the elements from that:
select extract (hour from numtodsinterval(timee, 'MINUTE')) || ' hours, '
|| extract (minute from numtodsinterval(timee, 'MINUTE')) || ' minutes, '
|| extract (second from numtodsinterval(timee, 'MINUTE')) || ' seconds' as xyme
from (select 125.58 as timee from dual);
XYME
----------------------------------------
2 hours, 5 minutes, 34.8 seconds
You can round or trunc the seconds value as appropriate; looks like you want round from your sample.

Related

SQL Query to Combine Dates

I have a data set that has dates like this:
MM DD YY
2 8 10
3 9 11
4 10 12
I'm trying to write a query that displays the dates in one single column as MM/DD/YY
Any help would be greatly appreciated thanks
If you dont mind the data format, you can concatenate them.
select MM || '/' || 'DD' || '/' || YY DATE_COL from T
you can also convert it to date
select to_Date(MM || '/' || 'DD' || '/' || YY, 'MM/DD/YY') DATE_COL from T
Teradata actually stores dates as integers, as explained in the documentation:
Teradata Database stores each DATE value as a four-byte integer using the following formula: (year - 1900) * 10000 + (month * 100) + day.
If you want to generate a result of date datatype from your numbers, you can just do:
cast((yy + 100) * 10000 + mm * 100 + dd as date)
On the other hand, if you just want a string in format mm/dd/yy, it is probably simpler to use string functions; we just need to pad the 1-digit values with 0:
lpad(mm, 2, '0') || '/' || lpad(dd, 2, '0') || '/' || lpad(yy, 2, '0')
You can do below, assuming your year is in the 21st century.
select to_Date(lpad(MM,2,'0')|| '/' ||lpad(DD,2,'0')|| '/20' ||lpad(YY,2,'0'),'MM/DD/YYYY') as Date_field
from my_Table;
Here is a demo in Oracle - https://dbfiddle.uk/?rdbms=oracle_11.2&fiddle=1ba6814d793f2b8b269611e348540f6c. This would work in Teradata as well.

Grouping SQL-Result by time does not work

I am trying to set up an SQL query in Firebird 2.5 in order to make a statistic about sales grouped by time.
My approach I followed up so far would be:
SELECT EXTRACT(WEEKDAY FROM ODR.ORDERDATE) AS S1, EXTRACT(HOUR FROM ODR.ORDERDATE) AS S2,
CASE
WHEN (EXTRACT(HOUR FROM ODR.ORDERDATE)) < 10 THEN
'0' || CAST(EXTRACT(HOUR FROM ODR.ORDERDATE) AS VARCHAR(2)) || ':00 - ' || '0' ||
CAST(EXTRACT(HOUR FROM ODR.ORDERDATE) AS VARCHAR(2)) || ':59 '
WHEN EXTRACT(HOUR FROM ODR.ORDERDATE) >= 10 THEN
CAST(EXTRACT(HOUR FROM ODR.ORDERDATE) AS VARCHAR(2)) || ':00 - ' ||
CAST(EXTRACT(HOUR FROM ODR.ORDERDATE) AS VARCHAR(2)) || ':59 '
END AS TINTERVAL,
COUNT(ODR.ID) AS ABSCOUNT
FROM ODR
GROUP BY S1,S2,TINTERVAL
ORDER BY S1,S2 ASC
The syntax of this query seems to be fine for SQL-Connection established by Firebird ODBC 2.5.
My question:
For hour=0 (<10) the query returns a result set which is not presented by the data given by the orders.
e.g. the result-set for this query might look like this:
S1 S2 TINTERVAL ABSCOUNT
0 0 00:00 - 00:59 30
0 1 01:00 - 01:59 2
I don't know how this query comes to its strange count in hour = 0.
This does not make any sense to me.

formatting total time spent using extract

I'm open to any suggestions, so long as I get something resembling the output at the bottom of the post, TIA!
Can I get some help with this sql fiddle: http://sqlfiddle.com/#!4/c51c5/1
SCHEMA
create table history(
clockingGroup varchar2(5)
, startTime timestamp
, endTime timestamp);
insert into history
(clockingGroup, startTime, endTime)
values
('grp1', '01-dec-2015 1:00:00.000000', '01-dec-2015 1:10:10.000000');
insert into history
(clockingGroup, startTime, endTime)
values
('grp2', '01-dec-2015 1:10:10.000000', '01-dec-2015 1:20:20.000000');
insert into history
(clockingGroup, startTime, endTime)
values
('grp1', '01-dec-2015 1:20:20.000000', '01-dec-2015 1:30:35.000000');
insert into history
(clockingGroup, startTime, endTime)
values
('grp3', '01-dec-2015 1:30:35.000000', '01-dec-2015 1:35:00.000000');
SQL
select
extract(hour from (sum(cast(endTime as date) - cast(startTime as date)))) || ' Hours '
|| extract(minute from (sum(cast(endTime as date) - cast(startTime as date)))) || ' Minutes '
|| extract(second from (sum(cast(endTime as date) - cast(startTime as date)))) || ' Seconds'
as totalTime
, clockingGroup
from
history
group by
clockingGroup
Current Error
ORA-30076: invalid extract field for extract source
My desired output is:
clockingGroup | totalTime
grp1 | 0 Hours 20 Minutes 25 Seconds
grp2 | 0 Hours 10 Minutes 10 Seconds
grp3 | 0 Hours 4 Minutes 25 Seconds
You are using the built ins in the wrong order. Your original query casts your timestamps to dates. When you subtract dates from each other you get a number.
In the query below it subtracts a timestamp from a timestamp which yields an INTERVAL. You cannot extract the hour from a number but you can from an INTERVAL.
SELECT clockingGroup.
SUM(extract(DAY FROM endtime-starttime)) ||' '||
SUM(extract(HOUR FROM endtime-starttime)) ||' '||
SUM(extract(MINUTE FROM endtime-starttime)) ||' '||
SUM(extract(SECOND FROM endtime-starttime)) AS TOTALTIME,
from
history
group by
clockingGroup
I see you have added another method using numtodsinterval. You are still converting timestamps to dates which you don't need to do if you want precision.
This query is more complicated as I try and show how you take the difference and count all the intervals as seconds, sum them and then break it apart into hours/minutes/seconds
SELECT CLOCKINGGROUP, TO_CHAR(TRUNC(SUMTOTALSECONDS/3600),'FM9900') || ' Hours ' ||
TO_CHAR(TRUNC(MOD(SUMTOTALSECONDS,3600)/60),'FM00') || ' Minutes ' ||
TO_CHAR(MOD(SUMTOTALSECONDS,60),'FM00') || ' Seconds'
FROM(
SELECT clockinggroup, TRUNC(SUM(TOTALSECONDS),0) AS SUMTOTALSECONDS
FROM (
SELECT clockinggroup,
EXTRACT (DAY FROM (EndTime-StartTime))*24*60*60 +
EXTRACT (HOUR FROM (EndTime-StartTime))*60*60 +
EXTRACT (MINUTE FROM (EndTime-StartTime))*60 +
EXTRACT (SECOND FROM (EndTime-StartTime))/60 AS TOTALSECONDS
FROM history)
group by
clockingGroup)
ORDER BY 1;

Oracle Convert Seconds to Hours:Minutes:Seconds

I have a requirement to display user available time in Hours:Minutes:Seconds format from a given total number of seconds value. Appreciate if you know a ORACLE function to do the same. I'm using Oracle.
Thank you for your time.
If you're just looking to convert a given number of seconds into HH:MI:SS format, this should do it
SELECT
TO_CHAR(TRUNC(x/3600),'FM9900') || ':' ||
TO_CHAR(TRUNC(MOD(x,3600)/60),'FM00') || ':' ||
TO_CHAR(MOD(x,60),'FM00')
FROM DUAL
where x is the number of seconds.
Try this one.
Very simple and easy to use
select to_char(to_date(10000,'sssss'),'hh24:mi:ss') from dual;
The following code is less complex and gives the same result. Note that 'X' is the number of seconds to be converted to hours.
In Oracle use:
SELECT TO_CHAR (TRUNC (SYSDATE) + NUMTODSINTERVAL (X, 'second'),
'hh24:mi:ss'
) hr
FROM DUAL;
In SqlServer use:
SELECT CONVERT(varchar, DATEADD(s, X, 0), 108);
If you have a variable containing f.e. 1 minute(in seconds), you can add it to the systimestamp then use to_char to select the different time parts from it.
select to_char(systimestamp+60/(24*60*60), 'yyyy.mm.dd HH24:mi:ss') from dual
For the comment on the answer by vogash, I understand that you want something like a time counter, thats because you can have more than 24 hours. For this you can do the following:
select to_char(trunc(xxx/3600)) || to_char(to_date(mod(xxx, 86400),'sssss'),':mi:ss') as time
from dual;
xxx are your number of seconds.
The first part accumulate the hours and the second part calculates the remaining minutes and seconds. For example, having 150023 seconds it will give you 41:40:23.
But if you always want have hh24:mi:ss even if you have more than 86000 seconds (1 day) you can do:
select to_char(to_date(mod(xxx, 86400),'sssss'),'hh24:mi:ss') as time
from dual;
xxx are your number of seconds.
For example, having 86402 seconds it will reset the time to 00:00:02.
Unfortunately not... However, there's a simple trick if it's going to be less than 24 hours.
Oracle assumes that a number added to a date is in days. Convert the number of seconds into days. Add the current day, then use the to_date function to take only the parts your interested in. Assuming you have x seconds:
select to_char(sysdate + (x / ( 60 * 60 * 24 ) ), 'HH24:MI:SS')
from dual
This won't work if there's more than 24 hours, though you can remove the current data again and get the difference in days, hours, minutes and seconds.
If you want something like: 51:10:05, i.e. 51 hours, 10 minutes and 5 seconds then you're going to have to use trunc.
Once again assuming that you have x seconds...
The number of hours is trunc(x / 60 / 60)
The number of minutes is trunc((x - ( trunc(x / 60 / 60) * 60 * 60 )) / 60)
The number of seconds is therefore the x - hours * 60 * 60 - minutes * 60
Leaving you with:
with hrs as (
select x, trunc(x / 60 / 60) as h
from dual
)
, mins as (
select x, h, trunc((x - h * 60 * 60) / 60) as m
from hrs
)
select h, m, x - (h * 60 * 60) - (m * 60)
from mins
I've set up a SQL Fiddle to demonstrate.
The following is Yet Another Way (tm) - still involves a little calculation but provides an example of using EXTRACT to pull the individual fields out of an INTERVAL:
DECLARE
SUBTYPE BIG_INTERVAL IS INTERVAL DAY(9) TO SECOND;
i BIG_INTERVAL;
nSeconds NUMBER := 86400000;
FUNCTION INTERVAL_TO_HMS_STRING(inv IN BIG_INTERVAL)
RETURN VARCHAR2
IS
nHours NUMBER;
nMinutes NUMBER;
nSeconds NUMBER;
strHour_format VARCHAR2(10) := '09';
workInv INTERVAL DAY(9) TO SECOND(9);
BEGIN
nHours := EXTRACT(HOUR FROM inv) + (EXTRACT(DAY FROM inv) * 24);
strHour_format := TRIM(RPAD(' ', LENGTH(TRIM(TO_CHAR(ABS(nHours)))), '0') || '9');
nMinutes := ABS(EXTRACT(MINUTE FROM inv));
nSeconds := ABS(EXTRACT(SECOND FROM inv));
RETURN TRIM(TO_CHAR(nHours, strHour_format)) || ':' ||
TRIM(TO_CHAR(nMInutes, '09')) || ':' ||
TRIM(TO_CHAR(nSeconds, '09'));
END INTERVAL_TO_HMS_STRING;
BEGIN
i := NUMTODSINTERVAL(nSeconds, 'SECOND');
DBMS_OUTPUT.PUT_LINE('i (fields) = ' || INTERVAL_TO_HMS_STRING(i));
END;
The code which extracts the fields, etc, still has to contain a calculation to convert the DAY field to equivalent hours, and is not the prettiest, but wrapped up neatly in a procedure it's not too bad to use.
Share and enjoy.
Assuming your time is called st.etime below and stored in seconds, here is what I use. This handles times where the seconds are greater than 86399 seconds (which is 11:59:59 pm)
case when st.etime > 86399 then to_char(to_date(st.etime - 86400,'sssss'),'HH24:MI:SS') else to_char(to_date(st.etime,'sssss'),'HH24:MI:SS') end readable_time
My version. Show Oracle DB uptime in format DDd HHh MMm SSs
select to_char(trunc((((86400*x)/60)/60)/24)) || 'd ' ||
to_char(trunc(((86400*x)/60)/60)-24*(trunc((((86400*x)/60)/60)/24)), 'FM00') || 'h ' ||
to_char(trunc((86400*x)/60)-60*(trunc(((86400*x)/60)/60)), 'FM00') || 'm ' ||
to_char(trunc(86400*x)-60*(trunc((86400*x)/60)), 'FM00') || 's' "UPTIME"
from (select (sysdate - t.startup_time) x from V$INSTANCE t);
idea from Date / Time Arithmetic with Oracle 9/10
Convert minutes to hour:min:sec format
SELECT
TO_CHAR(TRUNC((MINUTES * 60) / 3600), 'FM9900') || ':' ||
TO_CHAR(TRUNC(MOD((MINUTES * 60), 3600) / 60), 'FM00') || ':' ||
TO_CHAR(MOD((MINUTES * 60), 60), 'FM00') AS MIN_TO_HOUR FROM DUAL
For greater than 24 hours you can include days with the following query. The returned format is days:hh24:mi:ss
Query:
select trunc(trunc(sysdate) + numtodsinterval(9999999, 'second')) - trunc(sysdate) || ':' || to_char(trunc(sysdate) + numtodsinterval(9999999, 'second'), 'hh24:mi:ss') from dual;
Output:
115:17:46:39
create or replace procedure mili(num in number)
as
yr number;
yrsms number;
mon number;
monsms number;
wk number;
wksms number;
dy number;
dysms number;
hr number;
hrsms number;
mn number;
mnsms number;
sec number;
begin
yr := FLOOR(num/31556952000);
yrsms := mod(num, 31556952000);
mon := FLOOR(yrsms/2629746000);
monsms := mod(num,2629746000);
wk := FLOOR(monsms/(604800000));
wksms := mod(num,604800000);
dy := floor(wksms/ (24*60*60*1000));
dysms :=mod(num,24*60*60*1000);
hr := floor((dysms)/(60*60*1000));
hrsms := mod(num,60*60*1000);
mn := floor((hrsms)/(60*1000));
mnsms := mod(num,60*1000);
sec := floor((mnsms)/(1000));
dbms_output.put_line(' Year:'||yr||' Month:'||mon||' Week:'||wk||' Day:'||dy||' Hour:'||hr||' Min:'||mn||' Sec: '||sec);
end;
/
begin
mili(12345678904234);
end;
create or replace function `seconds_hh_mi_ss` (seconds in number)
return varchar2
is
hours_var number;
minutes_var number;
seconds_var number;
remeinder_var number;
output_var varchar2(32);
begin
select seconds - mod(seconds,3600) into hours_var from dual;
select seconds - hours_var into remeinder_var from dual;
select (remeinder_var - mod(remeinder_var,60)) into minutes_var from dual;
select seconds - (hours_var+minutes_var) into seconds_var from dual;
output_var := hours_var/3600||':'||minutes_var/60||':'||seconds_var;
return(output_var);
end;
/
You should check out this site. The TO_TIMESTAMP section could be useful for you!
Syntax:
TO_TIMESTAMP ( string , [ format_mask ] [ 'nlsparam' ] )

Convert number to time

How to convert number 1.33408564814814 to time 32:01:05?
If you actually want the result as a character string, you could use a function like this:
set serveroutput on format wrapped;
declare
function days_to_time (p_days number) return varchar2
is
d number := p_days;
h integer;
m integer;
s integer;
begin
h := trunc(d*24);
d := d - h/24;
m := trunc(d*24*60);
d := d - m/24/60;
s := round(d*24*60*60);
return(h||':'||to_char(m,'FM00')||':'||TO_CHAR(s,'FM00'));
end;
begin
dbms_output.put_line(days_to_time(1.33408564814814));
end;
/
anonymous block completed
32:01:05
SELECT
-- EXTRACT(day FROM numtodsinterval(1.33408564814814 , 'DAY')) Days,
EXTRACT(day FROM numtodsinterval(1.33408564814814 , 'DAY')) * 24 + EXTRACT(hour FROM numtodsinterval(1.33408564814814 , 'DAY')) TotalHours,
-- EXTRACT(hour FROM numtodsinterval(1.33408564814814 , 'DAY')) Hours,
EXTRACT(minute FROM numtodsinterval(1.33408564814814 , 'DAY')) Minutes,
EXTRACT(second FROM numtodsinterval(1.33408564814814 , 'DAY')) Seconds
FROM dual;
The original number is an interval expressed as a number of days. The following is a bit ugly but might help show what's going on:
SELECT days,
hours,
minutes,
seconds,
TO_CHAR(days, 'FM9') || ' ' ||
TO_CHAR(day_hours, 'FM09') || ':' ||
TO_CHAR(minutes, 'FM09') || ':' ||
TO_CHAR(seconds, 'FM09') AS interval_string,
TO_DSINTERVAL(TO_CHAR(days, 'FM9') || ' ' ||
TO_CHAR(day_hours, 'FM09') || ':' ||
TO_CHAR(minutes, 'FM09') || ':' ||
TO_CHAR(seconds, 'FM09')) actual_interval
FROM (SELECT float_val,
days, hours, minutes,
hours - (days * 24) AS day_hours,
ROUND((((decimal_hours - hours) * 60) - trunc((decimal_hours - hours) * 60)) * 60) AS seconds
FROM (SELECT float_val,
trunc(hours / 24) AS days,
decimal_hours,
hours,
(decimal_hours - hours) * 60 AS decimal_minutes,
trunc((decimal_hours - hours) * 60) AS minutes
FROM (SELECT float_val,
float_val * 24 AS decimal_hours,
TRUNC(float_val* 24) AS hours
FROM (SELECT 1.33408564814814 AS float_val FROM dual))));
Share and enjoy.