Search code examples
sqloracle11g

how to add the current date to time column in oracle sql


I have a table which hold only time part.

  TIME 
 08:00
 08:00
 08:00
 08:00
 18:00
 19:00
 20:00

Now, i need to concatenate the time with current date. for that i tried with below code.

to_date( to_char (sysdate,'YYYYMMDD')|| Time,'HH24MI')

but that is not working as expected and getting the below error

ORA-01830: date format picture ends before converting entire input string

Can you please help me to solve this problem. Expected output(date time format):

14-Feb-23 08:00
14-Feb-23 09:00

Solution

  • What datatype do you expect as result? A string?

    SQL> with test (time) as
      2    (select '08:00' from dual)
      3  select to_char(sysdate, 'dd-Mon-yy ') || time as result
      4  from test;
    
    RESULT
    ---------------
    14-Feb-23 08:00
    

    If you expect date, then

    SQL> with test (time) as
      2    (select '08:00' from dual)
      3  select to_date(to_char(sysdate, 'dd-Mon-yy ') || time, 'dd-Mon-yy hh24:mi') as result
      4  from test;
    
    RESULT
    --------
    14.02.23
    

    Hm, what format is that?! My default! Modify it to something else, e.g. what you said you want but - nonetheless - value previous code returns is a valid date value:

    SQL> alter session set nls_date_format = 'dd-Mon-yy hh24:mi';
    
    Session altered.
    
    SQL> with test (time) as
      2    (select '08:00' from dual)
      3  select to_date(to_char(sysdate, 'dd-Mon-yy ') || time, 'dd-Mon-yy hh24:mi') as result
      4  from test;
    
    RESULT
    ---------------
    14-Feb-23 08:00
    
    SQL>