Oracle SQL group row count by time intervals of 10 minutes - sql

I have a table in oracle which contains data such as the following
created_date details
01-Jan-16 04:45 abcd
01-Jan-16 04:47 efgh
01-Jan-16 04:53 ijkl
01-Jan-16 04:54 mnop
01-Jan-16 04:58 qrst
....etc
I want to be able to count the number of rows in the table for every 10 minutes
e.g.
Time count
04:40 2
04:50 3
Created Date = Timestamp,
details = varchar
How would i do this?
Thanks

You can use TO_CHAR and SUBSTR to build the time string:
select
substr(to_char(created_date, 'hh24:mi'), 1, 4) || '0' as created,
count(*)
from mytable
group by substr(to_char(created_date, 'hh24:mi'), 1, 4) || '0'
order by substr(to_char(created_date, 'hh24:mi'), 1, 4) || '0';
Or with a subquery (a derived table), so as to have to write the date expression only once:
select created, count(*)
from
(
select substr(to_char(created_date, 'hh24:mi'), 1, 4) || '0' as created
from mytable
)
group by created
order by created;

One method is to extract the hour and minute and do arithmetic:
select extract(hour from created_date) as hh,
floor(extract(minute from created_date) / 6) as min,
count(*)
from t
group by extract(hour from created_date),
floor(extract(minute from created_date) / 6)

An answer would be:
select trunc(sysdate, 'hh')+ trunc(to_char(sysdate,'mi')/10)*10/1440 from dual;
You can replace sysdate with your actual date/timestamp column and dual with your table
To understand the components, run:
select trunc(sysdate, 'hh') the_hour,
to_char(sysdate,'mi') the_minutes,
trunc(to_char(sysdate,'mi')/10)*10 minutes_truncated,
trunc(to_char(sysdate,'mi')/10)*10/1440 part_of_the_day, --as 1 represents a day in oracle datetime system
trunc(sysdate, 'hh')+ trunc(to_char(sysdate,'mi')/10)*10/1440 result
from dual;

Here's a solution if you want to make the group by on an actual timestamp value:
create table test_10_minutes_group_by (created_date timestamp, details varchar2(4));
insert into test_10_minutes_group_by values (systimestamp, 'aaa'); -- current time
insert into test_10_minutes_group_by values (systimestamp - 1/24/60, 'bbb'); -- 1 minute ago
insert into test_10_minutes_group_by values (systimestamp - 1/24/60 * 10, 'ccc'); -- 10 minutes ago
insert into test_10_minutes_group_by values (systimestamp - 1/24/60 * 20, 'ccc2'); -- 20 minutes ago
insert into test_10_minutes_group_by values (systimestamp - 1/24/60 * 25, 'abc'); -- 25 minutes ago
insert into test_10_minutes_group_by values (systimestamp - 1/24/60 * 30, 'xyz'); -- 30 minutes ago
insert into test_10_minutes_group_by values (systimestamp - 1/24/60 * 35, 'xyz2'); -- 35 minutes ago
select
actual_time,
to_char(actual_time, 'hh24:mi:ss') pretty_date,
count(1)
from (
select
trunc(created_date, 'mi') /*remove seconds*/ - 1/24/60 * mod(extract (minute from created_date), 10) /*substract units digit from minutes*/ actual_time,
details
from
test_10_minutes_group_by
)
group by actual_time;

Related

Sum column in data type varchar2 oracle 21c

My question was about the possibility of collecting the time column, especially since the data type is varchar2:
CREATE TABLE t_video
(
video_id NUMBER NOT NULL ENABLE,
video_duration VARCHAR2(30 BYTE),
object_video VARCHAR2(1000 BYTE),
CONSTRAINT T_VIDEO_PK PRIMARY KEY ( VIDEO_ID )
);
INSERT INTO t_video (video_id, video_duration, object_video)
VALUES (1,'00:12:20',song);
INSERT INTO t_video (video_id, video_duration, object_video)
VALUES (2,'02:50:30',film);
Then I tried and succeeded in solving the problem as follows:
-- code sum hours , minutes, seconds in three column
SELECT
SUM(to_char(substr(video_duration, - 8, 2))) AS hours,
SUM(to_char(substr(video_duration, - 5, 2))) / 60 AS minutes,
SUM(to_char(substr(video_duration, - 2, 2))) / 60 / 60 AS seconds
FROM
t_video;
-- code sum hours , minutes, seconds in one column
SELECT
id_user,
SUM(ROUND(h1 + h2 + h3, 2)) AS total_hours
FROM
(SELECT
id_user,
to_char(substr(video_duration, -8, 2)) AS h1,
to_char(substr(video_duration, -5, 2)) / 60 AS h2,
to_char(substr(video_duration, -2, 2)) / 60 / 60 AS h3
FROM
t_video)
GROUP BY
ROLLUP(id_user);
I converted the time to HH24 only and got the required result, which is as follows:
SELECT id_user,
CAST(SUM(
EXTRACT(HOUR FROM
video_duration) * 60 * 60 +
EXTRACT(MINUTE FROM
video_duration) * 60 +
EXTRACT(SECOND FROM
video_duration)) *
INTERVAL '24' SECOND AS
INTERVAL DAY(1) TO
SECOND(0)) AS
total_duration
FROM
t_video
GROUP BY ROLLUP(id_user);
RESULT
id_user video_duration
------- ------------------
10 + 241 07: 39: 36
------------------------------
20 + 75 13: 40: 00
------------------------------
NULL + 316 21: 19: 36
Thank you for your help. Thank you, site management, 🌹♥️
If you are storing times you can use the INTERVAL DAY(0) TO SECOND(0) data type (rather than strings) and then your query can be:
SELECT video_id,
SUM(
EXTRACT(HOUR FROM video_duration) * 60 * 60
+ EXTRACT(MINUTE FROM video_duration) * 60
+ EXTRACT(SECOND FROM video_duration)
) * INTERVAL '1' SECOND AS total_duration
FROM t_video
GROUP BY ROLLUP(video_id);
Which, for the sample data:
CREATE TABLE t_video
(
video_id NUMBER NOT NULL ENABLE,
video_duration INTERVAL DAY(0) TO SECOND(0),
object_video VARCHAR2(1000 BYTE),
CONSTRAINT T_VIDEO_PK PRIMARY KEY ( VIDEO_ID )
);
INSERT INTO t_video (video_id, video_duration, object_video)
VALUES (1, INTERVAL '00:12:20' HOUR TO SECOND,'song');
INSERT INTO t_video (video_id, video_duration, object_video)
VALUES (2, INTERVAL '02:50:30' HOUR TO SECOND,'film');
Outputs:
VIDEO_ID
TOTAL_DURATION
1
+000000000 00:12:20.000000000
2
+000000000 02:50:30.000000000
null
+000000000 03:02:50.000000000
If you want to format it differently you can cast from the default INTERVAL DAY(9) TO SECOND(9) to an interval with smaller precision such as INTERVAL DAY(1) TO SECOND(0):
SELECT video_id,
CAST(
SUM(
EXTRACT(HOUR FROM video_duration) * 60 * 60
+ EXTRACT(MINUTE FROM video_duration) * 60
+ EXTRACT(SECOND FROM video_duration)
) * INTERVAL '1' SECOND
AS INTERVAL DAY(1) TO SECOND (0)
) AS total_duration
FROM t_video
GROUP BY ROLLUP(video_id);
VIDEO_ID
TOTAL_DURATION
1
+0 00:12:20
2
+0 02:50:30
null
+0 03:02:50
fiddle
The best thing to do would be to change the type of the columns to interval day to second. Then you can use Oracle's built in time interval functions.
-- Make a new interval day to second column.
alter table t_video add video_duration_new interval day to second;
-- Translate your string durations into intervals.
-- to_dsinterval requires a day, so we add 0 days. This assumes all your video durations are all HH:MM:SS
update t_video set video_duration_new = to_dsinterval(concat('0 ', video_duration));
-- Drop the old column.
alter table t_video drop column video_duration;
-- Replace it with the interval column.
alter table t_video rename column video_duration_new to video_duration;
Now getting the number of hours is trivial using extract.
select extract(hour from video_duration) from t_video;
This can also be indexed for performance, and will check the values are formatted properly.
Demonstration.

How to get hour and min from timestamp variable in Oracle SQL

I got a timestamp variable like this:
INSERT INTO Table VALUES(TO_TIMESTAMP('2021-02-17 17:00','YYYY-MM-DD HH24:MI'));
I've created a trigger and my aim is to get the hour and min from that attribute so that I can compare them in two cursors like time.cur1 = time.cur2 (supposing that time is hour:min or hour+min), is there any cast like for the date,
CAST((timestamp) AS DATE)
should I create a new type after extracting the hour and min with
SELECT EXTRACT(HOUR FROM timestamp) FROM Table;
SELECT EXTRACT(MIN FROM timestamp) FROM Table;
or there is another way? (I'm using Oracle Database 11g). Thanks.
New type? No, put those values into a NUMBER datatype variables.
SQL> declare
2 l_hour number;
3 l_min number;
4 begin
5 select extract (hour from systimestamp),
6 extract (minute from systimestamp)
7 into l_hour,
8 l_min
9 from dual;
10
11 dbms_output.put_line('Hour: ' || l_hour ||'; minute: ' || l_min);
12 end;
13 /
Hour: 11; minute: 47
PL/SQL procedure successfully completed.
SQL>
SELECT TO_CHAR(SYSDATE, 'HH24') AS HOUR, TO_CHAR(SYSDATE, 'MI') AS MINUTS FROM DUAL;
TO YOUR EXAMPLE:
INSERT INTO Table VALUES(HOUR, MINUTS)
SELECT TO_CHAR(SYSDATE, 'HH24') AS HOUR, TO_CHAR(SYSDATE, 'MI') AS MINUTS;
FROM <EVERY_TABLE_OR_DUAL>;

SQL filter query output based on difference of two timestamp field [duplicate]

I have a table as follows:
Filename - varchar
Creation Date - Date format dd/mm/yyyy hh24:mi:ss
Oldest cdr date - Date format dd/mm/yyyy hh24:mi:ss
How can I calcuate the difference in hours minutes and seconds (and possibly days) between the two dates in Oracle SQL?
Thanks
You can substract dates in Oracle. This will give you the difference in days. Multiply by 24 to get hours, and so on.
SQL> select oldest - creation from my_table;
If your date is stored as character data, you have to convert it to a date type first.
SQL> select 24 * (to_date('2009-07-07 22:00', 'YYYY-MM-DD hh24:mi')
- to_date('2009-07-07 19:30', 'YYYY-MM-DD hh24:mi')) diff_hours
from dual;
DIFF_HOURS
----------
2.5
Note:
This answer applies to dates represented by the Oracle data type DATE.
Oracle also has a data type TIMESTAMP, which can also represent a date (with time). If you subtract TIMESTAMP values, you get an INTERVAL; to extract numeric values, use the EXTRACT function.
To get result in seconds:
select (END_DT - START_DT)*60*60*24 from MY_TABLE;
Check [https://community.oracle.com/thread/2145099?tstart=0][1]
select
extract( day from diff ) Days,
extract( hour from diff ) Hours,
extract( minute from diff ) Minutes
from (
select (CAST(creationdate as timestamp) - CAST(oldcreationdate as timestamp)) diff
from [TableName]
);
This will give you three columns as Days, Hours and Minutes.
declare
strTime1 varchar2(50) := '02/08/2013 01:09:42 PM';
strTime2 varchar2(50) := '02/08/2013 11:09:00 PM';
v_date1 date := to_date(strTime1,'DD/MM/YYYY HH:MI:SS PM');
v_date2 date := to_date(strTime2,'DD/MM/YYYY HH:MI:SS PM');
difrence_In_Hours number;
difrence_In_minutes number;
difrence_In_seconds number;
begin
difrence_In_Hours := (v_date2 - v_date1) * 24;
difrence_In_minutes := difrence_In_Hours * 60;
difrence_In_seconds := difrence_In_minutes * 60;
dbms_output.put_line(strTime1);
dbms_output.put_line(strTime2);
dbms_output.put_line('*******');
dbms_output.put_line('difrence_In_Hours : ' || difrence_In_Hours);
dbms_output.put_line('difrence_In_minutes: ' || difrence_In_minutes);
dbms_output.put_line('difrence_In_seconds: ' || difrence_In_seconds);
end ;
Hope this helps.
You may also try this:
select to_char(to_date('1970-01-01 00:00:00', 'yyyy-mm-dd hh24:mi:ss')+(end_date - start_date),'hh24:mi:ss')
as run_time from some_table;
It displays time in more human readable form, like: 00:01:34.
If you need also days you may simply add DD to last formatting string.
Calculate age from HIREDATE to system date of your computer
SELECT HIREDATE||' '||SYSDATE||' ' ||
TRUNC(MONTHS_BETWEEN(SYSDATE,HIREDATE)/12) ||' YEARS '||
TRUNC((MONTHS_BETWEEN(SYSDATE,HIREDATE))-(TRUNC(MONTHS_BETWEEN(SYSDATE,HIREDATE)/12)*12))||
'MONTHS' AS "AGE " FROM EMP;
You could use to_timestamp function to convert the dates to timestamps and perform a substract operation.
Something like:
SELECT
TO_TIMESTAMP ('13.10.1990 00:00:00','DD.MM.YYYY HH24:MI:SS') -
TO_TIMESTAMP ('01.01.1990:00:10:00','DD.MM.YYYY:HH24:MI:SS')
FROM DUAL
In oracle 11g
SELECT end_date - start_date AS day_diff FROM tablexxx
suppose the starT_date end_date is define in the tablexxx
select days||' '|| time from (
SELECT to_number( to_char(to_date('1','J') +
(CLOSED_DATE - CREATED_DATE), 'J') - 1) days,
to_char(to_date('00:00:00','HH24:MI:SS') +
(CLOSED_DATE - CREATED_DATE), 'HH24:MI:SS') time
FROM request where REQUEST_ID=158761088 );
If you want something that looks a bit simpler, try this for finding events in a table which occurred in the past 1 minute:
With this entry you can fiddle with the decimal values till you get the minute value that you want. The value .0007 happens to be 1 minute as far as the sysdate significant digits are concerned. You can use multiples of that to get any other value that you want:
select (sysdate - (sysdate - .0007)) * 1440 from dual;
Result is 1 (minute)
Then it is a simple matter to check for
select * from my_table where (sysdate - transdate) < .00071;
If you select two dates from 'your_table' and want too see the result as a single column output (eg. 'days - hh:mm:ss') you could use something like this.
First you could calculate the interval between these two dates and after that export all the data you need from that interval:
select extract (day from numtodsinterval (second_date
- add_months (created_date,
floor (months_between (second_date,created_date))),
'day'))
|| ' days - '
|| extract (hour from numtodsinterval (second_date
- add_months (created_date,
floor (months_between (second_date,created_date))),
'day'))
|| ':'
|| extract (minute from numtodsinterval (second_date
- add_months (created_date,
floor (months_between (second_date, created_date))),
'day'))
|| ':'
|| extract (second from numtodsinterval (second_date
- add_months (created_date,
floor (months_between (second_date, created_date))),
'day'))
from your_table
And that should give you result like this:
0 days - 1:14:55
select (floor(((DATE2-DATE1)*24*60*60)/3600)|| ' : ' ||floor((((DATE2-DATE1)*24*60*60) -floor(((DATE2-DATE1)*24*60*60)/3600)*3600)/60)|| ' ' ) as time_difference from TABLE1
(TO_DATE(:P_comapre_date_1, 'dd-mm-yyyy hh24:mi') - TO_DATE(:P_comapre_date_2, 'dd-mm-yyyy hh24:mi'))*60*60*24 sum_seconds,
(TO_DATE(:P_comapre_date_1, 'dd-mm-yyyy hh24:mi') - TO_DATE(:P_comapre_date_2, 'dd-mm-yyyy hh24:mi'))*60*24 sum_minutes,
(TO_DATE(:P_comapre_date_1, 'dd-mm-yyyy hh24:mi') - TO_DATE(:P_comapre_date_2, 'dd-mm-yyyy hh24:mi'))*24 sum_hours,
(TO_DATE(:P_comapre_date_1, 'dd-mm-yyyy hh24:mi') - TO_DATE(:P_comapre_date_2, 'dd-mm-yyyy hh24:mi')) sum_days
select to_char(actual_start_date,'DD-MON-YYYY hh24:mi:ss') start_time,
to_char(actual_completion_date,'DD-MON-YYYY hh24:mi:ss') end_time,
floor((actual_completion_date-actual_start_date)*24*60)||'.'||round(mod((actual_completion_date-actual_start_date)*24*60*60,60)) diff_time
from fnd_concurrent_requests
order by request_id desc;
If You want get date defer from using table and column.
SELECT TO_DATE( TO_CHAR(COLUMN_NAME_1, 'YYYY-MM-DD'), 'YYYY-MM-DD') -
TO_DATE(TO_CHAR(COLUMN_NAME_2, 'YYYY-MM-DD') , 'YYYY-MM-DD') AS DATEDIFF
FROM TABLE_NAME;
This will count time between to dates:
SELECT
(TO_CHAR( TRUNC (ROUND(((sysdate+1) - sysdate)*24,2))*60,'999999')
+
TO_CHAR(((((sysdate+1)-sysdate)*24)- TRUNC(ROUND(((sysdate+1) - sysdate)*24,2)))/100*60 *100, '09'))/60
FROM dual
Here's another option:
with tbl_demo AS
(SELECT TO_DATE('11/26/2013 13:18:50', 'MM/DD/YYYY HH24:MI:SS') dt1
, TO_DATE('11/28/2013 21:59:12', 'MM/DD/YYYY HH24:MI:SS') dt2
FROM dual)
SELECT dt1
, dt2
, round(dt2 - dt1,2) diff_days
, round(dt2 - dt1,2)*24 diff_hrs
, numtodsinterval((dt2 - dt1),'day') diff_dd_hh_mm_ss
from tbl_demo;
Single query that will return time difference of two timestamp columns:
select INS_TS, MAIL_SENT_TS, extract( hour from (INS_TS - MAIL_SENT_TS) ) timeDiff
from MAIL_NOTIFICATIONS;
select round( (tbl.Todate - tbl.fromDate) * 24 * 60 * 60 )
from table tbl
for oracle sql I justbn did this and works perfect :
SELECT trunc(date_col_1) - trunc(date_col_2)
FROM TABLE;
$sql="select bsp_bp,user_name,status,
to_char(ins_date,'dd/mm/yyyy hh12:mi:ss AM'),
to_char(pickup_date,'dd/mm/yyyy hh12:mi:ss AM'),
trunc((pickup_date-ins_date)*24*60*60,2),message,status_message
from valid_bsp_req where id >= '$id'";

Subtracting the difference between two time intervals in oracle sql [duplicate]

I have a table as follows:
Filename - varchar
Creation Date - Date format dd/mm/yyyy hh24:mi:ss
Oldest cdr date - Date format dd/mm/yyyy hh24:mi:ss
How can I calcuate the difference in hours minutes and seconds (and possibly days) between the two dates in Oracle SQL?
Thanks
You can substract dates in Oracle. This will give you the difference in days. Multiply by 24 to get hours, and so on.
SQL> select oldest - creation from my_table;
If your date is stored as character data, you have to convert it to a date type first.
SQL> select 24 * (to_date('2009-07-07 22:00', 'YYYY-MM-DD hh24:mi')
- to_date('2009-07-07 19:30', 'YYYY-MM-DD hh24:mi')) diff_hours
from dual;
DIFF_HOURS
----------
2.5
Note:
This answer applies to dates represented by the Oracle data type DATE.
Oracle also has a data type TIMESTAMP, which can also represent a date (with time). If you subtract TIMESTAMP values, you get an INTERVAL; to extract numeric values, use the EXTRACT function.
To get result in seconds:
select (END_DT - START_DT)*60*60*24 from MY_TABLE;
Check [https://community.oracle.com/thread/2145099?tstart=0][1]
select
extract( day from diff ) Days,
extract( hour from diff ) Hours,
extract( minute from diff ) Minutes
from (
select (CAST(creationdate as timestamp) - CAST(oldcreationdate as timestamp)) diff
from [TableName]
);
This will give you three columns as Days, Hours and Minutes.
declare
strTime1 varchar2(50) := '02/08/2013 01:09:42 PM';
strTime2 varchar2(50) := '02/08/2013 11:09:00 PM';
v_date1 date := to_date(strTime1,'DD/MM/YYYY HH:MI:SS PM');
v_date2 date := to_date(strTime2,'DD/MM/YYYY HH:MI:SS PM');
difrence_In_Hours number;
difrence_In_minutes number;
difrence_In_seconds number;
begin
difrence_In_Hours := (v_date2 - v_date1) * 24;
difrence_In_minutes := difrence_In_Hours * 60;
difrence_In_seconds := difrence_In_minutes * 60;
dbms_output.put_line(strTime1);
dbms_output.put_line(strTime2);
dbms_output.put_line('*******');
dbms_output.put_line('difrence_In_Hours : ' || difrence_In_Hours);
dbms_output.put_line('difrence_In_minutes: ' || difrence_In_minutes);
dbms_output.put_line('difrence_In_seconds: ' || difrence_In_seconds);
end ;
Hope this helps.
You may also try this:
select to_char(to_date('1970-01-01 00:00:00', 'yyyy-mm-dd hh24:mi:ss')+(end_date - start_date),'hh24:mi:ss')
as run_time from some_table;
It displays time in more human readable form, like: 00:01:34.
If you need also days you may simply add DD to last formatting string.
Calculate age from HIREDATE to system date of your computer
SELECT HIREDATE||' '||SYSDATE||' ' ||
TRUNC(MONTHS_BETWEEN(SYSDATE,HIREDATE)/12) ||' YEARS '||
TRUNC((MONTHS_BETWEEN(SYSDATE,HIREDATE))-(TRUNC(MONTHS_BETWEEN(SYSDATE,HIREDATE)/12)*12))||
'MONTHS' AS "AGE " FROM EMP;
You could use to_timestamp function to convert the dates to timestamps and perform a substract operation.
Something like:
SELECT
TO_TIMESTAMP ('13.10.1990 00:00:00','DD.MM.YYYY HH24:MI:SS') -
TO_TIMESTAMP ('01.01.1990:00:10:00','DD.MM.YYYY:HH24:MI:SS')
FROM DUAL
In oracle 11g
SELECT end_date - start_date AS day_diff FROM tablexxx
suppose the starT_date end_date is define in the tablexxx
select days||' '|| time from (
SELECT to_number( to_char(to_date('1','J') +
(CLOSED_DATE - CREATED_DATE), 'J') - 1) days,
to_char(to_date('00:00:00','HH24:MI:SS') +
(CLOSED_DATE - CREATED_DATE), 'HH24:MI:SS') time
FROM request where REQUEST_ID=158761088 );
If you want something that looks a bit simpler, try this for finding events in a table which occurred in the past 1 minute:
With this entry you can fiddle with the decimal values till you get the minute value that you want. The value .0007 happens to be 1 minute as far as the sysdate significant digits are concerned. You can use multiples of that to get any other value that you want:
select (sysdate - (sysdate - .0007)) * 1440 from dual;
Result is 1 (minute)
Then it is a simple matter to check for
select * from my_table where (sysdate - transdate) < .00071;
If you select two dates from 'your_table' and want too see the result as a single column output (eg. 'days - hh:mm:ss') you could use something like this.
First you could calculate the interval between these two dates and after that export all the data you need from that interval:
select extract (day from numtodsinterval (second_date
- add_months (created_date,
floor (months_between (second_date,created_date))),
'day'))
|| ' days - '
|| extract (hour from numtodsinterval (second_date
- add_months (created_date,
floor (months_between (second_date,created_date))),
'day'))
|| ':'
|| extract (minute from numtodsinterval (second_date
- add_months (created_date,
floor (months_between (second_date, created_date))),
'day'))
|| ':'
|| extract (second from numtodsinterval (second_date
- add_months (created_date,
floor (months_between (second_date, created_date))),
'day'))
from your_table
And that should give you result like this:
0 days - 1:14:55
select (floor(((DATE2-DATE1)*24*60*60)/3600)|| ' : ' ||floor((((DATE2-DATE1)*24*60*60) -floor(((DATE2-DATE1)*24*60*60)/3600)*3600)/60)|| ' ' ) as time_difference from TABLE1
(TO_DATE(:P_comapre_date_1, 'dd-mm-yyyy hh24:mi') - TO_DATE(:P_comapre_date_2, 'dd-mm-yyyy hh24:mi'))*60*60*24 sum_seconds,
(TO_DATE(:P_comapre_date_1, 'dd-mm-yyyy hh24:mi') - TO_DATE(:P_comapre_date_2, 'dd-mm-yyyy hh24:mi'))*60*24 sum_minutes,
(TO_DATE(:P_comapre_date_1, 'dd-mm-yyyy hh24:mi') - TO_DATE(:P_comapre_date_2, 'dd-mm-yyyy hh24:mi'))*24 sum_hours,
(TO_DATE(:P_comapre_date_1, 'dd-mm-yyyy hh24:mi') - TO_DATE(:P_comapre_date_2, 'dd-mm-yyyy hh24:mi')) sum_days
select to_char(actual_start_date,'DD-MON-YYYY hh24:mi:ss') start_time,
to_char(actual_completion_date,'DD-MON-YYYY hh24:mi:ss') end_time,
floor((actual_completion_date-actual_start_date)*24*60)||'.'||round(mod((actual_completion_date-actual_start_date)*24*60*60,60)) diff_time
from fnd_concurrent_requests
order by request_id desc;
If You want get date defer from using table and column.
SELECT TO_DATE( TO_CHAR(COLUMN_NAME_1, 'YYYY-MM-DD'), 'YYYY-MM-DD') -
TO_DATE(TO_CHAR(COLUMN_NAME_2, 'YYYY-MM-DD') , 'YYYY-MM-DD') AS DATEDIFF
FROM TABLE_NAME;
This will count time between to dates:
SELECT
(TO_CHAR( TRUNC (ROUND(((sysdate+1) - sysdate)*24,2))*60,'999999')
+
TO_CHAR(((((sysdate+1)-sysdate)*24)- TRUNC(ROUND(((sysdate+1) - sysdate)*24,2)))/100*60 *100, '09'))/60
FROM dual
Here's another option:
with tbl_demo AS
(SELECT TO_DATE('11/26/2013 13:18:50', 'MM/DD/YYYY HH24:MI:SS') dt1
, TO_DATE('11/28/2013 21:59:12', 'MM/DD/YYYY HH24:MI:SS') dt2
FROM dual)
SELECT dt1
, dt2
, round(dt2 - dt1,2) diff_days
, round(dt2 - dt1,2)*24 diff_hrs
, numtodsinterval((dt2 - dt1),'day') diff_dd_hh_mm_ss
from tbl_demo;
Single query that will return time difference of two timestamp columns:
select INS_TS, MAIL_SENT_TS, extract( hour from (INS_TS - MAIL_SENT_TS) ) timeDiff
from MAIL_NOTIFICATIONS;
select round( (tbl.Todate - tbl.fromDate) * 24 * 60 * 60 )
from table tbl
for oracle sql I justbn did this and works perfect :
SELECT trunc(date_col_1) - trunc(date_col_2)
FROM TABLE;
$sql="select bsp_bp,user_name,status,
to_char(ins_date,'dd/mm/yyyy hh12:mi:ss AM'),
to_char(pickup_date,'dd/mm/yyyy hh12:mi:ss AM'),
trunc((pickup_date-ins_date)*24*60*60,2),message,status_message
from valid_bsp_req where id >= '$id'";

Help with Sql query for comparing string(HH24MI) to date

We have a configuration table as shown below that stores the start time and the duration.
If the start time is 9:20 pm (3rd one ) add the duration then the time becomes 9:35.
I have to find out if the current time is in between any of the values.
I have to return the output based on the start_time and duration. i.e current time should be between start_time and the start_time + duration. (between 09:20 and and 09:35)
Can you please help me with the sql query or is it better if we go with sql function?
Start_time, duration(minutes) output
1108 5 2
1054 100 5
2120 15 8
I'm not a fan of storing dates and times in VARCHAR2 columns. START_TIME should really be a DATE or a TIMESTAMP column.
That said, you can do something like
with x as (
select '1108' start_time, 5 duration, 2 output from dual
union all
select '1054', 100, 5 from dual
union all
select '2120', 15, 8 from dual
)
select *
from (
select to_date(
to_char(sysdate,'YYYY-MM-DD') || ' ' ||
start_time,
'YYYY-MM-DD HH24MI' ) start_date,
to_date(
to_char(sysdate,'YYYY-MM-DD') || ' ' ||
start_time,
'YYYY-MM-DD HH24MI' ) + duration/24/60 end_date
from x)
where sysdate between start_date and end_date
The following selects all rows where sysdate is within the Start_Time and Start_Time + duration (EDITed as per comment from OP):
SELECT (TRUNC ( SYSDATE ) + TO_NUMBER ( SUBSTR ( Start_Time, 0, 2 ) ) / 24.0 + TO_NUMBER ( SUBSTR ( Start_Time, 3 ) ) / (24.0 * 60.0)) start_date, (TRUNC ( SYSDATE ) + TO_NUMBER ( SUBSTR ( Start_Time, 0, 2 ) ) / 24.0 + TO_NUMBER ( SUBSTR ( Start_Time, 3 ) ) / (24.0 * 60.0) + TO_NUMBER (duration)) end_date FROM configtable;