I have an Oracle DB with a timestamp
field in it. What is the correct SQL code to insert a timestamp
into this field?
insert
into tablename (timestamp_value)
values (TO_TIMESTAMP(:ts_val, 'YYYY-MM-DD HH24:MI:SS'));
if you want the current time stamp to be inserted then:
insert
into tablename (timestamp_value)
values (CURRENT_TIMESTAMP);
CREATE TABLE Table1 (
id int identity(1, 1) NOT NULL,
Somecolmn varchar (5),
LastChanged [timestamp] NOT NULL)
this works for mssql 2012
INSERT INTO Table1 VALUES('hello',DEFAULT)
For my own future reference:
With cx_Oracle use cursor.setinputsize(...):
mycursor = connection.cursor();
mycursor.setinputsize( mytimestamp=cx_Oracle.TIMESTAMP );
params = { 'mytimestamp': timestampVar };
cusrsor.execute("INSERT INTO mytable (timestamp_field9 VALUES(:mytimestamp)", params);
No converting in the db needed. See Oracle Documentation
I prefer ANSI timestamp literals:
insert into the_table
(the_timestamp_column)
values
(timestamp '2017-10-12 21:22:23');
More details in the manual: https://docs.oracle.com/database/121/SQLRF/sql_elements003.htm#SQLRF51062
INSERT
INTO mytable (timestamp_field)
VALUES (CURRENT_TIMESTAMP)
CURRENT_TIMESTAMP
and SYSTIMESTAMP
are Oracle reserved words for this purpose. They are the timestamp analog of SYSDATE
.
Inserting date in sql
insert
into tablename (timestamp_value)
values ('dd-mm-yyyy hh-mm-ss AM');
If suppose we wanted to insert system date
insert
into tablename (timestamp_value)
values (sysdate);