Subtracting the difference between two time intervals in oracle sql [duplicate] - sql
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'";
Related
Oracle difference between 2 dates whose datatype is CHAR(23 BYTE)
How to find diff between below 2 dates in minutes, both data types is CHAR using SQL 2019-10-31 10:23:05.927 2019-10-30 11:20:05.500
You can use to_date() to turn both strings to dates (ignoring the millisecond part), then subtract them. This gives you a numeric value that represents the difference between the dates as a number of days, that you can then convert to hours: ( to_date(substr(col1, 1, 19), 'yyyy-mm-dd hh24:mi:ss') - to_date(substr(col2, 1, 19), 'yyyy-mm-dd hh24:mi:ss') ) * 24 * 60 If you do want to include the millisecond part, then it gets lengthy. You can convert the strings to timestamps using to_timestamp(), then substract them; this produces an interval, from which you need to extract each part. extract(hour from to_timestamp(col1, 'yyyy-mm-dd hh24:mi:ss.ff3') - to_timestamp(col1, 'yyyy-mm-dd hh24:mi:ss.ff3') ) * 60 + extract(minute from to_timestamp(col1, 'yyyy-mm-dd hh24:mi:ss.ff3') - to_timestamp(col1, 'yyyy-mm-dd hh24:mi:ss.ff3') ) + extract(second from to_timestamp(col1, 'yyyy-mm-dd hh24:mi:ss.ff3') - to_timestamp(col1, 'yyyy-mm-dd hh24:mi:ss.ff3') ) / 60
ORACLE SQL - How to check whether a time falls within a particular range?
There is a column 'DateTime' that displays the Date and Time in 'YYYY/MM/DD HH24:MI:SS' format. I need to display only the rows that has time in a particular range(2AM - 6AM) regardless of the particular date. I have a code that only displays the time range(2AM - 6AM) for the particular day. I need to display the rows that falls in the past 7 days with the time range 2AM-6AM. SELECT * FROM <table_name > WHERE DateTime BETWEEN TO_DATE ( TO_CHAR (TRUNC (SYSDATE), 'DD-MM-YYYY') || ' ' || '02:00:00', 'DD-MM-YYYY HH24:MI:SS') AND TO_DATE ( TO_CHAR (TRUNC (SYSDATE), 'DD-MM-YYYY') || ' ' || '06:00:00', 'DD-MM-YYYY HH24:MI:SS') ORDER BY 1 DESC
SELECT * FROM your_table WHERE TO_CHAR( DateTime, 'HH24MMSS' ) BETWEEN '020000' AND '060000' AND DateTime >= TRUNC( SYSDATE ) - INTERVAL '7' DAY AND DateTime < TRUNC( SYSDATE ) + INTERVAL '1' DAY or SELECT * FROM your_table WHERE DateTime BETWEEN TRUNC( DateTime ) + INTERVAL '2' HOUR AND TRUNC( DateTime ) + INTERVAL '6' HOUR AND DateTime >= TRUNC( SYSDATE ) - INTERVAL '7' DAY AND DateTime < TRUNC( SYSDATE ) + INTERVAL '1' DAY
You need two conditions for that: one for the hour being between 02 and 05, and one for the day being in the last seven days: SELECT * FROM <table_name> WHERE to_char(DateTime, 'HH') BETWEEN '02' and '05' AND DateTime BETWEEN SYSDATE - 7 AND SYSDATE ORDER BY 1 DESC
In this case, I think the easiest way is string comparison: WHERE SUBSTR(DateTime, 12, 2) BETWEEN '02' and '05' Note: Between is inclusive so this should get everything up to 05:59:59.
SELECT * FROM <table_name > WHERE DateTime BETWEEN TO_DATE ( TO_CHAR (TRUNC (SYSDATE)-7, 'DD-MM-YYYY') || ' ' || '02:00:00', 'DD-MM-YYYY HH24:MI:SS') AND TO_DATE ( TO_CHAR (TRUNC (SYSDATE), 'DD-MM-YYYY') || ' ' || '06:00:00', 'DD-MM-YYYY HH24:MI:SS')
select * from table_name where to_char(datetime,'HH24') between '2' and '6' and to_char(datetime,'dd-mon-yy') between to_char(sysdate,'dd-mon-yy') and to_char(sysdate-7,'dd-mon-yy');
Massive insert sql - Oracle 11g
I need a query to insert rows into a table within the last N days from today. Insert into Table select 'xpto', name from users where login_date between TO_DATE(:DATE || ' 00:00:00', 'mm/dd/yyyy HH24:MI:SS') and TO_DATE(:DATE || ' 23:59:59', 'mm/dd/yyyy HH24:MI:SS') I need this :DATE to be represented by last 30 days... (each day).. So it would be 30 inserts. How can I do that? EDIT Lets say I want to insert data from the last 30 days... So we have: 01/18/2016 01/19/2016 01/20/2016 ... 02/01/2016 02/02/2016 02/03/2016 .... 02/15/2016 .... 02/22/2016 I want a single query - or statement - to insert my data based on each days above like this: Insert into Table select 'xpto', name from users where login_date between TO_DATE('01/15/2016' || ' 00:00:00', 'mm/dd/yyyy HH24:MI:SS') and TO_DATE('01/15/2016' || ' 23:59:59', 'mm/dd/yyyy HH24:MI:SS') --- another insert Insert into Table select 'xpto', name from users where login_date between TO_DATE('01/16/2016' || ' 00:00:00', 'mm/dd/yyyy HH24:MI:SS') and TO_DATE('01/16/2016' || ' 23:59:59', 'mm/dd/yyyy HH24:MI:SS') I don't want to do one query for each day... ANOTHER EDIT I'm sorry I just got my work notebook here...here is the real sample: BEGIN for day in (SELECT to_char(TO_DATE (SYSDATE, 'dd/mm/yyyy')-30 + LEVEL) AS DATE_CHECK FROM DUAL CONNECT BY SYSDATE - 30 + LEVEL <= SYSDATE) LOOP v_date := to_char(day.date_check); INSERT INTO resume (date_check, type, total) SELECT v_data AS DATA, type, COUNT (*) total FROM ( select .... from table WHERE DATE_COLUMN BETWEEN TO_DATE(v_data ||' 00:00:00', 'dd/mm/yyyy hh24:mi:ss') and TO_DATE(v_date || ' 23:59:59', 'dd/mm/yyyy hh24:mi:ss') union select .... from table WHERE DATE_COLUMN BETWEEN TO_DATE(v_data ||' 00:00:00', 'dd/mm/yyyy hh24:mi:ss') and TO_DATE(v_date || ' 23:59:59', 'dd/mm/yyyy hh24:mi:ss') ) end loop; end; but it doesn't work... if I do the same insert manually replacing v_date by any date (14/02/2016) it works.... :(
If I understand what you're trying to do correctly the following might serve: Insert into Table select 'xpto', name from users where login_date between TRUNC(SYSDATE) - INTERVAL '30' DAY and TRUNC(SYSDATE) + INTERVAL '1' DAY - INTERVAL '1' SECOND EDIT Based on the edit to the question it appears that we can just expand the range, as in: Insert into Table select 'xpto', name from users where login_date between TO_DATE('01/18/2016', 'MM/DD/YYYY') and TO_DATE('02/22/2016', 'MM/DD/YYYY') + INTERVAL '1' DAY - INTERVAL '1' SECOND SECOND EDIT Thank you for clarifying. Perhaps the following will help: BEGIN for day in (SELECT TRUNC(SYSDATE)-30 + LEVEL AS DATE_CHECK FROM DUAL CONNECT BY TRUNC(SYSDATE) - 30 + LEVEL <= TRUNC(SYSDATE)) LOOP INSERT INTO resume (date_check, type, total) SELECT day.DATE_CHECK AS DATA, type, COUNT (*) total FROM (select .... from table WHERE DATE_COLUMN = TRUNC(day.DATE_CHECK)); end loop; end; Best of luck.
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'";
Difference between two dates in oracle [duplicate]
This question already has answers here: Calculate difference between 2 date / times in Oracle SQL (21 answers) Closed 9 years ago. How to find difference between two dates in oracle? I tried with Extract method but it is not working properly. Format should be hh:mm:ss
EXTRACT works on datetime or interval value expression. When you subtract two dates, you get a number which is the number fo days. If you want to use extract you should convert it into interval datatype using NUMTODSINTERVAL and then use EXTRACT. SELECT TO_DATE ('14-01-2014 14:00:00', 'dd-mm-yyyy hh24:mi:ss') - TO_DATE ('13-01-2014 22:30:45', 'dd-mm-yyyy hh24:mi:ss') FROM DUAL; 0.6453125 SELECT NUMTODSINTERVAL ( TO_DATE ('14-01-2014 14:00:00', 'dd-mm-yyyy hh24:mi:ss') - TO_DATE ('13-01-2014 22:30:45', 'dd-mm-yyyy hh24:mi:ss'), 'DAY') FROM DUAL; +00 15:29:15.000000 SELECT EXTRACT (HOUR FROM intrvl) || ':' || EXTRACT (MINUTE FROM intrvl) || ':' || EXTRACT (SECOND FROM intrvl) FROM (SELECT NUMTODSINTERVAL ( TO_DATE ('14-01-2014 14:00:00', 'dd-mm-yyyy hh24:mi:ss') - TO_DATE ('13-01-2014 22:30:45', 'dd-mm-yyyy hh24:mi:ss'), 'DAY') AS intrvl FROM DUAL); 15:29:15 You can even cast the date as timestamp, so that the difference is already in interval datatype. SELECT EXTRACT (HOUR FROM intrvl) || ':' || EXTRACT (MINUTE FROM intrvl) || ':' || EXTRACT (SECOND FROM intrvl) FROM (SELECT CAST ( TO_DATE ('14-01-2014 14:00:00', 'dd-mm-yyyy hh24:mi:ss') AS TIMESTAMP) - CAST ( TO_DATE ('13-01-2014 22:30:45', 'dd-mm-yyyy hh24:mi:ss') AS TIMESTAMP) AS intrvl FROM DUAL);