Skip to Main Content

Java Database Connectivity (JDBC)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

How to convert number of hours to date format?

843854Sep 21 2004 — edited Jun 2 2006
Let's say i have a table named tblDate and has 2 columns: date_FirstDate and date_Hours.

im asking how do u convert number of hours to a date format in sql query?

Let's say the value of date_FirstDate is 09-21-2004 09:22:54 AM and the value of date_Hours is 72.
what i want is to convert the date_FirstDate into number of hours then add it to date_Hours. and then i want to convert back the total of hours to date format.

How will i do that in sql query statement?

i know this is possible in sql coz getting the difference between two dates can be done in sql:
DATEDIFF(date1, date2, hh)

Comments

843854
anyone?
i need ur help guys.
843854
To add time to a date just use eg. sysdate + 10/24

Using '+' will add days onto a date. So to add a number of hours u add '1/24 * x' where x is the number of hours.

So u want ....
select date_FirstDate + date_Hours/24
from tblDate 
Ted.
843854
no im not gona add two dates, what i want is i want to convert the date format into number of hours then add it to date_Hours which is plus 72 then convert again the sum or total numbers in date.

or the other way around, i can convert the 72 hours to date format and thenadd it to the date_FirstDate.
843854
You haven't tried my idea have you??

You dont only use '+' for adding JUST dates. If you do 'date + number' it will treat the number as 'number of days' NOT A DATE!!!!!!

I'll run you through the logic...
1. 1 day = 24 hours
2. Divide 1 by 24, gives you value for one hour
3. Multiply this figure by the number of hours required (in your example 72)
4. Add this figure onto your date
5. Voila!

heres a screen shot from a sql session...
SQL> select to_char(sysdate, 'dd-mm-yy hh24:mi:ss') from dual;

TO_CHAR(SYSDATE,'
-----------------
22-09-04 13:45:48

SQL> ed
Wrote file afiedt.buf

  1* select to_char(sysdate + 72/24, 'dd-mm-yy hh24:mi:ss') from dual
SQL> /

TO_CHAR(SYSDATE+7
-----------------
25-09-04 13:46:07
You will notice that this added 3 days on. The time difference (19 seconds) is because im using sysdate.
843854
iv tried it but it doesnt work.
ok for a clearer view of what i want to happen, here's an example below:

I have a table named tblDate that has 2 columns: column_DateReported (data type char) and column_Hour data type int.

Let's say tblDate has 1 row:


column_DateReported_________column_Hour
09-23-2004 09:32:55 AM_____________72


what i want to happen is to convert column_DateReported to number of hours then add it to column_Hour which is 72 then get the total then convert the total to date format 09-26-2004 09:32:55 AM.
or you can do the other way around, convert the column_Hour 72 to date format then add it to column_DateReported.

72 hours is equivalent to 3 days, so if u add the two, you'll get 09-26-2004 09:32:55 AM
and if i changed 72 to 10 hours then i should get 09-23-2004 07:32:55 PM

Iv tried
1.)
SELECT DATEADD(hh, column_DateReported, column_Hour) AS totaldate FROM tblDate
but this code is adding two numbers where column_DateReported and column_Hour's data type must be int.

2.)
SELECT (column_DateReported + 72/24) AS totaldate FROM tblDate
this doesn't work, null pointer error.

843854
someone replied to my post in the other forum, and the code he gave me works.

here's the code::
SELECT CONVERT(varchar(25),DATEADD(hh,column_Hour,column_DateReported)) AS totaldate FROM tblDate
thanks guys! :)
843854
My solution does work, at least I have had no problems. Was it just the date format that wasnt correct?

What databse are you using as that solution isnt very sql-92?
843854
im using sql server 2000.
843859
I have similar problem with date and time.
In Oracle 9 i have a table: TABLE1(id,dt)
dt data type: number for example:
id dt
1 1134481032
in sql query :
select (to_date('1.1.1970 00:00:00','dd.mm.yyyy hh24:mi:ss') + (dt/60/60/24))
from TABLE1 where dt = '1134481032'
i get : 13/12/05 13:37:12
till now eveything is ok but when i create a jtable and importing this query i get only: 13/12/2005 (IF I CHANGE THE DATA TYPE TO DATE)
or 13/12/2005 12:00:00 am (IF I CHANGE THE DATA TYPE TO TIMESTAMP)
i have try everything but every time i recieve the same ( 13/12/2005 12:00:00 am)
Please i need help. It will be nice if someone can give me some code as example.
Thanks
1 - 9
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Jun 30 2006
Added on Sep 21 2004
9 comments
751 views