How to insert a timestamp in Oracle?

后端 未结 10 1711
广开言路
广开言路 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:28

    First of all you need to make the field Nullable, then after that so simple - instead of putting a value put this code CURRENT_TIMESTAMP.

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

    Kind of depends on where the value you want to insert is coming from. If you want to insert the current time you can use CURRENT_TIMESTAMP as shown in other answers (or SYSTIMESTAMP).

    If you have a time as a string and want to convert it to a timestamp, use an expression like

    to_timestamp(:timestamp_as_string,'MM/DD/YYYY HH24:MI:SS.FF3')
    

    The time format components are, I hope, self-explanatory, except that FF3 means 3 digits of sub-second precision. You can go as high as 6 digits of precision.

    If you are inserting from an application, the best answer may depend on how the date/time value is stored in your language. For instance you can map certain Java objects directly to a TIMESTAMP column, but you need to understand the JDBC type mappings.

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

    One can simply use

    INSERT INTO MY_TABLE(MY_TIMESTAMP_FIELD)
    VALUES (TIMESTAMP '2019-02-15 13:22:11.871+02:00');
    

    This way you won't have to worry about date format string, just use default timestamp format.

    Works with Oracle 11, have no idea if it does for earlier Oracle versions.

    0 讨论(0)
  • 2020-12-23 03:34
    INSERT INTO TABLE_NAME (TIMESTAMP_VALUE) VALUES (TO_TIMESTAMP('2014-07-02 06:14:00.742000000', 'YYYY-MM-DD HH24:MI:SS.FF'));
    
    0 讨论(0)
提交回复
热议问题