Skip to Main Content

SQL & PL/SQL

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.

Adding hours to a date

user5716448Jun 23 2021

Hi,

version 11.2

Have a date column and need to look up another table which has two columns both varchar2
example

date

23/06/2021

table looking up has start 18:00 and extra 00:10

need to add bot these to existing one such that result would be 23/06/2021 18:10

How is this best achieved?

Thanks

Comments

User_H3J7U
to_date('23/06/2021'||'18:00'||'00:10', 'dd/mm/yyyyhh24":0000:"mi')
Frank Kulash

HI, @porourke
As mentioned already, use TO_DATE. If you need help, post a little sample data (CREATE TABLE and INSERT statements) and the exact results you want from that data.

BluShadow

Adding hours to a date (0 Bytes)
You say that the two times are a start and an additional time. The first answer given to you assumes that the first time is the "hours" and the second time is the "minutes". I'm not sure that's what you want.
Better not to hack the strings together with concatenation, but to turn them in to intervals and add them on that way, using proper date functionality that Oracle is so good at... e.g.

SQL> with t1 (dt) as (select date '2021-06-23' from dual) -- table with date column
  2      ,t2 (strt, ad) as ( -- table with two times "start" and "additional" to be added
  3       -- This assumes that both times can contain hours and minutes
  4       -- not just that the first time is hours and the second time is minutes
  5       select '18:00', '00:10' from dual union all
  6       select '16:30', '02:45' from dual
  7      )
  8  -- end of test data
  9  select t1.dt
 10        ,t2.strt
 11        ,t2.ad
 12        ,t1.dt
 13         +(to_date(t2.strt,'HH24:MI')-trunc(sysdate,'MM')) -- interval for start time
 14         +(to_date(t2.ad,'HH24:MI')-trunc(sysdate,'MM')) -- interval for additional time
 15         as result
 16  from   t1
 17         cross join t2
 18  /


DT                   STRT  AD    RESULT
-------------------- ----- ----- --------------------
23-JUN-2021 00:00:00 18:00 00:10 23-JUN-2021 18:10:00
23-JUN-2021 00:00:00 16:30 02:45 23-JUN-2021 19:15:00

By turning the time string in to a date with format mask HH24:MI it picks up the ~current date~ first day of the current month by default, so then subtracting the truncated current date (which eliminates any time component) leaves you with an interval that represents the time period, which you can then add to whatever date you want.
Do this with both of the times and you get both intervals added to the date correctly. If both intervals added together exceed 24 hours then the date will correctly roll over to the next day.
[Edited to correct]

User_H3J7U

By turning the time string in to a date with format mask HH24:MI it picks up the ~current date~ by default
First day of current month.

BluShadow

You are correct... my mistake. Working on too many things at once. ;)

user5716448

Thanks for updates

1 - 6

Post Details

Added on Jun 23 2021
6 comments
224 views