views:

99

answers:

1

Hi,

I need to figure how much time passed between 2 times. For example:

(14:00:00 - 13:15:00) * 24 = .75

I need this to later on convert KW to KWh, but that's not the point.

I can not find out how to do this in PL/SQL.

My date/time fields look like this in the DB:

1/23/2010 21:00:00

I would appreciate any suggestions.

Steve

+1  A: 

If you are using 9i above does this give you the expected result?

select extract(hour from numtodsinterval(to_date('14:00:00','HH24:MI:SS') - to_date('13:15:00','HH24:MI:SS'),'DAY'))
||':'|| extract(minute from numtodsinterval(to_date('14:00:00','HH24:MI:SS') - to_date('13:15:00','HH24:MI:SS'),'DAY')) diff
from dual
/

DIFF
------------------

0:45

Extract is detailed here

and numtodsinterval is detailed here

carpenteri
Thank you very much for the fast solution. Now I am strugling with my own data isues I think. I am getting this error: "ORA-01858: a non-numeric character was found where a numeric was expected" - The dateTime field looks like this: 8/12/2009 14:29:59 - I don't get it
Steve
Glad I could help - Instead of to_date('14:00:00','HH24:MI:SS')how about using to_date('8/12/2009 14:29:59', 'MM/DD/YYYY HH24:MI:SS')I don't know if I have got the month (MM) and the day (DD) in the correct position but you should be able to swap these
carpenteri
Thanks again, I did just that but the weird thing is if I hardcode the date/time into the function it works, when I get it from the table I got that error. The date is copied from the table, how can that be....anyway, you already help me so much. Thank you again.
Steve
if you run this query: select valuefrom nls_session_parameterswhere parameter = 'NLS_DATE_FORMAT' what date format do you get back? if it is different to what you expect and the database setting try using ALTER SESSION SET NLS_DATE_FORMAT='YOUR DATE FORMAT'
carpenteri
I get this: DD-MON-RR
Steve
I don't have toad so if you run this command in sql plus what do you get? show parameter nls_date_format
carpenteri
That did it but now the DIFF = 0.0 - Should be 0.75 - My STart = 8/12/2009 13:15:00 - endtime = 8/12/2009 14:29:59
Steve
Sorry man, I did not properly set the NLS format, after setting it correctly its all good. Sorry about that. This was a great learning experience.
Steve
No worries and again glad I could help!
carpenteri