Sub topic 13
Going Further: Timestamps with TO_TIMESTAMP and TO_CHAR
Assume you know SELECT, format models, and the Oracle DATE type. This chapter focuses on TIMESTAMP, which stores a date and time with fractional seconds. A DATE stores seconds but no fractional seconds. A TIMESTAMP can store up to nine fractional-second digits.
Why use TIMESTAMP?
Use TIMESTAMP when the time below one second matters, such as event logging, measurements, or transaction ordering.
A timestamp value is not automatically text. Oracle stores it as a temporal value, so you can compare it chronologically and use timestamp arithmetic. Convert it to text only when displaying it or exchanging it with a system that expects a specific string format.
The TO_TIMESTAMP function parses character data into a TIMESTAMP. The TO_CHAR function formats a timestamp as character data.
SELECT TO_CHAR(
TO_TIMESTAMP(
'2026-11-28 14:30:15.123456',
'YYYY-MM-DD HH24:MI:SS.FF6'
),
'YYYY-MM-DD HH24:MI:SS.FF3'
) AS timestamp_text
FROM dual;The input format uses FF6 because the input has six fractional-second digits. The output uses FF3, so the displayed value contains three digits:
2026-11-28 14:30:15.123The formatting operation does not change the stored timestamp. It changes only the returned string.
Reading timestamp format models
A timestamp format model combines date and time elements.
| Element | Meaning | Example |
|---|---|---|
| `YYYY-MM-DD` | Four-digit year, month, and day | `2026-11-28` |
| `HH24:MI:SS` | 24-hour time, minutes, and seconds | `14:30:15` |
| `FF6` | Six fractional-second digits | `.123456` |
| `TZH:TZM` | Time-zone hour and minute offset | `+05:30` |
The format model must match the input string. Literal punctuation usually must appear in the same positions. For example, 14:30 requires HH24:MI, while 2:30 PM requires HH:MI PM.
Use HH24 for a 24-hour clock. Do not combine HH24 with AM or PM.
Fractional seconds and precision
A timestamp column has a declared precision. TIMESTAMP(6) is common and stores six fractional-second digits. If you omit the precision, Oracle uses its default precision.
CREATE TABLE api_events (
event_id NUMBER,
occurred_at TIMESTAMP(6)
);
INSERT INTO api_events (event_id, occurred_at)
VALUES (
1,
TO_TIMESTAMP('2026-11-28 14:30:15.123456',
'YYYY-MM-DD HH24:MI:SS.FF6')
);
SELECT event_id,
TO_CHAR(occurred_at, 'YYYY-MM-DD HH24:MI:SS.FF6') AS occurred_text
FROM api_events;If a value has more fractional-second digits than the column can store, Oracle rounds or adjusts the value to the column's precision. Choose a precision that matches the accuracy your application needs.
Timestamps and time zones
TIMESTAMP does not store a time-zone offset. If the offset or region is part of the meaning, use TIMESTAMP WITH TIME ZONE.
For example, 2026-11-28 14:30:00 +05:30 identifies both a local time and its offset. A plain TIMESTAMP stores only 2026-11-28 14:30:00, so the original offset is unavailable.
SELECT TO_CHAR(
TO_TIMESTAMP_TZ(
'2026-11-28 14:30:15.123456 +05:30',
'YYYY-MM-DD HH24:MI:SS.FF6 TZH:TZM'
),
'YYYY-MM-DD HH24:MI:SS.FF6 TZH:TZM'
) AS timestamp_with_zone
FROM dual;Use TO_TIMESTAMP_TZ when parsing a string that includes a time-zone offset or region. Do not discard the zone before deciding whether the application needs it.
A timestamp conversion path
Exercise
Which function and format model parse 2026-11-28 14:30:15.123456 into a timestamp with six fractional-second digits?
Show answer
Use TO_TIMESTAMP('2026-11-28 14:30:15.123456', 'YYYY-MM-DD HH24:MI:SS.FF6').