Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
like image 683
Tikkaty Avatar asked Oct 04 '15 22:10

Tikkaty


People also ask

How can I insert current date and time in a table in SQL?

The simplest method to insert the current date and time in MySQL is to use the now() function. Once you call the function, it returns the current date and time in the system's configured time zone as a string. The value returned from the now() function is YYYY-MM-DD for the date and HH-MM-SS-UU for the time record.

How do I add a TIMESTAMP to a table in Oracle?

You can use the below code: insert into tablename (timestamp_value) values (TO_TIMESTAMP(:ts_val, 'YYYY-MM-DD HH24:MI:SS')); If you need the current timestamp to be inserted then use the following code: insert into tablename (timestamp_value) values (CURRENT_TIMESTAMP);

How do I insert a Sysdate into a table?

To insert a SYSDATE value in Oracle, you can just insert the SYSDATE function into a DATE column. This is the ideal method because both the SYSDATE and the DATE column are in a date datatype. There's no need to convert when inserting into the table.

Can we insert TIMESTAMP in date column in Oracle?

Insert the date as a TIMESTAMP literal. Oracle drops the time zone information. SQL> INSERT INTO table_dt VALUES(3, TIMESTAMP '2003-01-01 00:00:00 US/Pacific'); Insert the date with the TO_DATE function.


1 Answers

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;
like image 131
Gordon Linoff Avatar answered Oct 18 '22 02:10

Gordon Linoff