Oracle TIMESTAMP

Summary: in this tutorial, you’ll learn about the Oracle TIMESTAMP data type and how to handle TIMESTAMP data effectively in the Oracle Database.

Introduction to Oracle TIMESTAMP data type #

The TIMESTAMP data type allows you to store date and time data, including year, month, day, hour, minute, second, and fractional seconds.

To define a TIMESTAMP column, you use the following syntax:

column_name TIMESTAMP[(fractional_seconds_precision)]Code language: SQL (Structured Query Language) (sql)

In this syntax, the fractional_seconds_precision specifies the number of digits in the fractional part of the SECOND field. It ranges from 0 to 9, meaning you can use the TIMESTAMP data type to store up to nanoseconds.

If you omit the fractional_seconds_precision, it defaults to 6.

The following shows how to define a TIMESTAMP column:

started_at TIMESTAMP(2)Code language: SQL (Structured Query Language) (sql)

In this example, the started_at column is a TIMESTAMP column with fractional seconds precision set to the microsecond.

Oracle TIMESTAMP literals #

To specify TIMESTAMP literals, you use the following format:

TIMESTAMP 'YYYY-MM-DD HH24:MI:SS.FF'Code language: SQL (Structured Query Language) (sql)

The following example illustrates a TIMESTAMP literal:

TIMESTAMP '1999-12-31 23:59:59.10'Code language: SQL (Structured Query Language) (sql)

For detailed information on constructing the format string, please check the Oracle date format.

Oracle TIMESTAMP examples #

First, create a new table calledlogs that has a TIMESTAMP column:

CREATE TABLE logs (
    log_id NUMBER GENERATED BY DEFAULT AS IDENTITY,
    message VARCHAR(255) NOT NULL,
    logged_at TIMESTAMP (2) NOT NULL,
    PRIMARY KEY (log_id)
);Code language: SQL (Structured Query Language) (sql)

Second, insert new rows into to the logs table:

INSERT INTO logs (
    message,
    logged_at
    )
VALUES (
    'Invalid username/password for root user',
    LOCALTIMESTAMP(2)
    );

INSERT INTO logs (
    message,
    logged_at
    )
VALUES (
    'User root logged in successfully',
    LOCALTIMESTAMP(2)
    );Code language: SQL (Structured Query Language) (sql)

In this example, we got the current local timestamp with the precision if two fractional seconds from the LOCALTIMESTAMP(2) function and inserted that value into the logged_at column of the logs table.

Third, query the TIMESTAMP data from the logs table:

SELECT log_id,
       message,
       logged_at
FROM logs;Code language: SQL (Structured Query Language) (sql)
Oracle TIMESTAMP querying data example

Formatting TIMESTAMP values #

To change the output of a TIMESTAMP value, you use the TO_CHAR() function by passing the name of TIMESTAMP value or column as the first argument, and the format string as the second argument.

The following statement uses the TO_CHAR() function to format values in the logged_at column:

SELECT message,
    TO_CHAR(logged_at, 'MONTH DD, YYYY "at" HH24:MI')
FROM logs;
Code language: SQL (Structured Query Language) (sql)

Output:

Oracle TIMESTAMP format using TO_CHAR

Extracting TIMESTAMP components #

To extract components a TIMESTAMP such as year, month, day, hour, minute, and second, you use the EXTRACT() function:

EXTRACT( component FROM timestamp);Code language: SQL (Structured Query Language) (sql)

For example:

SELECT 
    message,
    EXTRACT(year FROM logged_at) year,
    EXTRACT(month FROM logged_at) month,
    EXTRACT(day FROM logged_at) day,
    EXTRACT(hour FROM logged_at) hour,
    EXTRACT(minute FROM logged_at) minute,
    EXTRACT(second FROM logged_at) second
FROM 
    logs;Code language: SQL (Structured Query Language) (sql)

Noted that the NLS_DATE_LANGUAGE parameter determines the language for the day names (Thursday), the month names (August), and the abbreviations (THU, AUG) of TIMESTAMP.

Default TIMESTAMP format #

Oracle uses the NLS_TIMESTAMP_FORMAT parameter to control the default timestamp format when a value of the character type is converted to the TIMESTAMP data type.

The following statement returns the current default timestamp format in the Oracle Database system:

SELECT
  value
FROM
  V$NLS_PARAMETERS
WHERE
  parameter = 'NLS_TIMESTAMP_FORMAT';Code language: SQL (Structured Query Language) (sql)

Here is the output:

DD-MON-RR HH.MI.SSXFF AMCode language: SQL (Structured Query Language) (sql)

If you want to insert a new row into the logs table with the logged_at value as a string, you can use the TO_TIMESTAMP() function to convert the string to a TIMESTAMP value as follows:

INSERT INTO logs (
    message,
    logged_at
    )
VALUES (
    'Test default Oracle timestamp format',
    TO_TIMESTAMP('03-AUG-17 11:20:30.45 AM')
    );
Code language: SQL (Structured Query Language) (sql)

Notice that the timestamp value '03-AUG-17 11:20:30.45 AM' follows the standard timestamp format.

Let’s verify the result:

SELECT
  log_id, message, logged_at
FROM
  logs;Code language: SQL (Structured Query Language) (sql)
Oracle TIMESTAMP default format demo

If you want to use another timestamp format instead of the default one, you can use the ALTER SESSION SET statement to do so.

Adding/subtracting an interval to/from a timestamp #

The following example adds 2 days to a timestamp '2017-08-03 10:30:20':

SELECT (TIMESTAMP '2017-08-03 10:30:20'  + interval '2' day) result
FROM dual;Code language: SQL (Structured Query Language) (sql)

Output:

RESULT                         
-------------------------------
05-AUG-17 10.30.20.000000000 AMCode language: SQL (Structured Query Language) (sql)

The following statement subtracts 3 days from a timestamp '2017-08-03 10:30:20' :

SELECT (TIMESTAMP '2017-08-03 10:30:20'  - interval '3' day) result
FROM dual;Code language: SQL (Structured Query Language) (sql)

Output:

RESULT                         
-------------------------------
31-JUL-17 10.30.20.000000000 AMCode language: SQL (Structured Query Language) (sql)

Subtracting two timestamps #

When you subtract two timestamps, you’ll get an interval. For example:

SELECT
  timestamp '2017-08-03 11:40:40' - timestamp '2017-08-02 10:30:20' RESULT;Code language: SQL (Structured Query Language) (sql)

Output:

RESULT             
-------------------
+01 01:10:20.000000Code language: SQL (Structured Query Language) (sql)

The result is an INTERVAL DAY TO SECOND.

Summary #

  • Use the Oracle TIMESTAMP data type to represent timestamp values.
Was this tutorial helpful?