How to insert a timestamp in Oracle?

后端 未结 10 1710
广开言路
广开言路 2020-12-23 02:34

I have an Oracle DB with a timestamp field in it. What is the correct SQL code to insert a timestamp into this field?

相关标签:
10条回答
  • 2020-12-23 03:08
    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);
    
    0 讨论(0)
  • 2020-12-23 03:10
    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)  
    
    0 讨论(0)
  • 2020-12-23 03:15

    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

    0 讨论(0)
  • 2020-12-23 03:21

    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

    0 讨论(0)
  • 2020-12-23 03:24
    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.

    0 讨论(0)
  • 2020-12-23 03:24

    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);
    
    0 讨论(0)
提交回复
热议问题