Tikkaty
Tikkaty

Reputation: 792

In Oracle SQL: How do you insert the current date + time into a table?

I've written below code, but it only seems to insert the current date and not the current time. Anyone knows how to do that?

insert into errortable
(dateupdated,table1id)
values
(TO_DATE(sysdate, 'dd/mm/yyyy hh24:mi:ss'),1083);

Upvotes: 29

Views: 179172

Answers (2)

brenners1302
brenners1302

Reputation: 1478

You may try with below query :

INSERT INTO errortable (dateupdated,table1id)
VALUES (to_date(to_char(sysdate,'dd/mon/yyyy hh24:mi:ss'), 'dd/mm/yyyy hh24:mi:ss' ),1083 );

To view the result of it:

SELECT to_char(hire_dateupdated, 'dd/mm/yyyy hh24:mi:ss') 
FROM errortable 
    WHERE table1id = 1083;

Upvotes: -2

Gordon Linoff
Gordon Linoff

Reputation: 1269513

It only seems to because that is what it is printing out. But actually, you shouldn't write the logic this way. This is equivalent:

insert into errortable (dateupdated, table1id)
    values (sysdate, 1083);

It seems silly to convert the system date to a string just to convert it back to a date.

If you want to see the full date, then you can do:

select TO_CHAR(dateupdated, 'YYYY-MM-DD HH24:MI:SS'), table1id
from errortable;

Upvotes: 37

Related Questions