oracle query with drive hour and capacity calculation

http://www.experts-exchange.com/Database/Oracle/Q_28541483.html

Closed

It was my mistake when I explained as to what was required.

drop table tab1 purge;
create table tab1(start_time date, end_time date,machines number);

Insert into TAB1
   (start_TIME, end_TIME,machines)
 Values
   (
   TO_DATE('10/01/2014 14:00:00', 'MM/DD/YYYY HH24:MI:SS'),
   TO_DATE('10/01/2014 19:30:00', 'MM/DD/YYYY HH24:MI:SS'),2);

commit;


select to_char(start_time,'HH12:MI:SS AM') starttime,
          to_char(end_time,'HH12:MI:SS AM') endtime from tab1

Now I want the total drive hours end_time minus start_time ( in the format 5.5)
or 4  hour drive as 4

and the hours has to be divided by 2 and always round it down. This number has to be multiplied by no of machines used to draw. Now I get the capacity.

capacity = (hours /2 ) * machines
LVL 6
anumosesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

anumosesAuthor Commented:
select  TRIM(TO_CHAR(TRUNC(((86400*(end_time - start_time))/60)/60)-24*(trunc((((86400*(end_time - start_time))/60)/60)/24)),'00')) ||'.'||
    TRIM(TO_CHAR(TRUNC((86400*(end_time - start_time))/60)-60*(trunc(((86400*(end_time - start_time))/60)/60)),'00')) ||'.'  as drive_hours

Tried but did not get correct number.
0
Alexander Eßer [Alex140181]Software DeveloperCommented:
select (((end_time - start_time) * 24) / 2) * machines
  from tab1;

Open in new window


btw: what exactly do you expect as result?!
0
anumosesAuthor Commented:
I just need the drive hours in number format. For the example above I need as 5.5
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

anumosesAuthor Commented:
The problem in my calculation is the date part in my database is not correct. If the drive is for today the date part says some day in 2010. So I have to only take hours and minutes in calculation the drive hours.
0
anumosesAuthor Commented:
CREATE TABLE TAB1
(
  DRIVE_DATE   DATE                             NOT NULL,
  DRIVE_ID     NUMBER(7)                        NOT NULL,
  START_TIME   VARCHAR2(8 BYTE),
  END_TIME     VARCHAR2(8 BYTE),
  DRIVE_HOURS  NUMBER
)




Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/01/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2024984, '10:30:00', '02:30:00', 4);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/01/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2025401, '10:00:00', '02:00:00', 4);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/01/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2024089, '08:30:00', '03:00:00', 6.5);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/01/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2025416, '10:00:00', '02:00:00', 4);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/01/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2024983, '03:30:00', '07:30:00', -22628);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/01/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2023342, '10:00:00', '02:00:00', -16076);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/01/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2025327, '09:00:00', '03:30:00', 6.5);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/01/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2025216, '10:00:00', '01:30:00', 3.5);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/01/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2025299, '12:00:00', '04:00:00', 4);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/02/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2025033, '08:30:00', '02:00:00', 5.5);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/02/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2023796, '02:00:00', '06:30:00', 24844.5);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/02/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2024586, '08:30:00', '12:30:00', 4);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/02/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2024985, '08:00:00', '02:00:00', 6);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/02/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2023436, '03:30:00', '07:30:00', 4);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/03/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2024986, '08:00:00', '12:00:00', -59900);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/03/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2025313, '01:00:00', '05:00:00', 4);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/03/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2025329, '10:00:00', '02:00:00', 4);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/03/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2024988, '08:50:00', '01:50:00', 5);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/04/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2025023, '09:30:00', '02:00:00', 4.5);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/04/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2023322, '10:00:00', '02:00:00', 8764);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/04/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2025302, '10:00:00', '02:00:00', 4);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/04/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2024779, '07:30:00', '11:30:00', 24100);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/04/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2024990, '09:00:00', '01:00:00', -19004);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/04/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2025292, '10:00:00', '02:00:00', 4);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/04/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2025213, '09:30:00', '01:30:00', 4);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/05/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2023320, '08:00:00', '12:30:00', 4.5);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/05/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2022795, '09:00:00', '01:00:00', 4);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/05/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2023279, '08:00:00', '11:00:00', 3);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/05/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2023486, '08:00:00', '12:30:00', 10228.5);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/05/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2022848, '08:00:00', '12:00:00', 4);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/05/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2023482, '08:00:00', '12:00:00', 4);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/06/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2022915, '09:30:00', '12:30:00', 3);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/06/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2025334, '01:30:00', '06:00:00', 4.5);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/06/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2022856, '03:00:00', '07:00:00', 4);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/06/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2023399, '03:30:00', '07:30:00', 4);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/07/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2025462, '04:00:00', '07:30:00', 3.5);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/07/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2023224, '03:30:00', '07:30:00', 4);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/07/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2025448, '03:00:00', '07:00:00', 4);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/07/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2025659, '03:00:00', '07:00:00', 4);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/07/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2024994, '11:00:00', '04:00:00', 725);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/07/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2024978, '04:00:00', '07:30:00', 3.5);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/07/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2025369, '11:00:00', '03:00:00', 4);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/07/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2024995, '09:00:00', '01:00:00', 4);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/07/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2025408, '10:00:00', '04:00:00', 6);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/07/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2024024, '08:15:00', '02:00:00', -20418.25);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/07/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2025012, '08:00:00', '01:45:00', -20490.25);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/08/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2025018, '08:30:00', '01:00:00', 4.5);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/08/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2024997, '02:00:00', '07:30:00', 5.5);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/08/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2025648, '10:00:00', '02:00:00', 4);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/08/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2024998, '07:00:00', '12:00:00', 5);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/08/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2025000, '08:00:00', '02:00:00', 6);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/08/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2025001, '01:00:00', '07:00:00', 6);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/08/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2022936, '08:00:00', '12:00:00', 9508);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/08/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2025017, '08:00:00', '01:45:00', -20490.25);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/08/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2025002, '10:30:00', '01:15:00', 2.75);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/09/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2024908, '09:00:00', '03:00:00', 6);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/09/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2025189, '03:00:00', '07:00:00', 4);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/09/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2025004, '08:30:00', '02:30:00', 6);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/09/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2025005, '10:00:00', '02:00:00', -9500);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/09/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2025221, '03:30:00', '07:00:00', 3.5);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/09/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2024944, '03:30:00', '07:30:00', 4);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/09/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2024405, '02:00:00', '06:00:00', 4);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/09/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2023140, '11:00:00', '04:00:00', 5);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/09/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2025452, '03:30:00', '07:30:00', 4);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/10/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2024613, '07:30:00', '01:30:00', 6);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/10/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2024686, '09:00:00', '01:00:00', 4);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/10/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2024619, '08:30:00', '01:30:00', 4421);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/11/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2025746, '10:00:00', '02:00:00', 4);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/11/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2023466, '08:00:00', '12:00:00', 4);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/11/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2025008, '09:00:00', '01:00:00', 4);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/11/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2025321, '12:30:00', '04:30:00', 4);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/11/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2025319, '10:00:00', '02:00:00', 4);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/11/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2023188, '07:30:00', '11:30:00', 4);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/11/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2022794, '09:00:00', '01:00:00', 4);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/11/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2025317, '09:00:00', '02:00:00', -739);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/11/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2024949, '08:00:00', '12:00:00', 4);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/12/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2025391, '08:00:00', '12:30:00', 1468.5);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/12/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2022599, '09:00:00', '01:00:00', 4);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/12/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2024462, '08:30:00', '12:30:00', 4);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/12/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2025373, '08:30:00', '12:00:00', 3.5);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/12/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2023182, '08:00:00', '12:30:00', 13852.5);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/13/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2025009, '03:30:00', '07:30:00', 4);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/13/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2022947, '10:00:00', '03:00:00', 5);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/13/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2025003, '09:00:00', '03:00:00', 17526);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/13/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2025839, '03:30:00', '07:30:00', 4);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/13/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2025295, '03:30:00', '07:30:00', 4);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/13/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2025010, '03:00:00', '07:00:00', 4);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/14/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2024853, '07:30:00', '02:00:00', 6.5);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/14/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2025397, '12:15:00', '05:15:00', 5);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/14/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2025011, '10:00:00', '02:00:00', 4);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/14/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2023704, '03:15:00', '07:30:00', -50395.75);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/14/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2023710, '08:00:00', '02:00:00', 6);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/14/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2025188, '09:00:00', '01:00:00', 4);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/14/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2024996, '07:45:00', '12:45:00', 5);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/14/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2024801, '08:00:00', '12:00:00', 4);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/15/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2025013, '11:00:00', '02:00:00', 3);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/15/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2025014, '10:00:00', '03:00:00', 7301);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/15/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2025015, '01:30:00', '07:30:00', -9498);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/15/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2025016, '10:00:00', '02:00:00', 4);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/15/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2023138, '12:30:00', '04:30:00', 4);
Insert into TAB1
   (DRIVE_DATE, DRIVE_ID, START_TIME, END_TIME, DRIVE_HOURS)
 Values
   (TO_DATE('10/15/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2025423, '10:30:00', '02:30:00', 4);
COMMIT;

Open in new window


If you look at the data I am having problem when start time is 3.30 and end time is 7.30 I need am or pm here. That is why (end_time-start_time)*24 drive_hours is not working for me
0
slightwv (䄆 Netminder) Commented:
Take all your previous questions about date and time manipulation.
Think about what the results you are wanting and formulate what you need to do to get the results.

From the last question:
You have the 'hours' and 'minutes'.
Minutes to fractional hours is minutes/60.

You get 5.5 from the first example, simple tweak from the SQL in the last question:
select 
  trim(extract(hour from numtodsinterval(to_date(to_char(end_time,'HH24:MI:SS'),'HH24:MI:SS')-to_date(to_char(start_time,'HH24:MI:SS'),'HH24:MI:SS'),'day'))) || 
  case when trim(to_char(extract(minute from numtodsinterval(to_date(to_char(end_time,'HH24:MI:SS'),'HH24:MI:SS')-to_date(to_char(start_time,'HH24:MI:SS'),'HH24:MI:SS'),'day')),'00'))  > 0 then 
  trim(to_char(extract(minute from numtodsinterval(to_date(to_char(end_time,'HH24:MI:SS'),'HH24:MI:SS')-to_date(to_char(start_time,'HH24:MI:SS'),'HH24:MI:SS'),'day')),'00'))/60
  end  mytime
from tab1
/
                                          

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
awking00Commented:
If your start times and end times are truly date datatypes, then use them to do your date math calculations and only use to_char for purposes of displaying the results. Please post a describe of your table and the results of the following query:

select start_time, to_char(start_time,'DD-MON-YYYY') display_start, end_time, to_char(end_time,'DD-MON-YYYY') display_end
from yourtable;

It's probably better for you to show us exactly what you're dealing with, then we can ask the questions to best determine what you need to do.
0
slightwv (䄆 Netminder) Commented:
awking00,
Read the previous question.

The data is all messed up.  The columns are date data types but the actual date portion doesn't matter and the start date can be after the end date.

Only the time portion of the column matters.
0
anumosesAuthor Commented:
Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.