Sub topic 13
Going further: timestamps
This chapter assumes you have read the earlier chapters on format models, TO_CHAR, TO_DATE, NLS settings, and conversion errors. Timestamps reuse those ideas and add fractional seconds and time zones. The main difference is that some DATE rules no longer apply.
DATE versus the timestamp types
A DATE stores the century, year, month, day, hour, minute, and second. It has no fractional seconds and no time zone. Oracle offers three timestamp types for finer detail.
| Type | Fractional seconds | Time zone stored | Typical use |
|---|---|---|---|
| `DATE` | No | No | Business dates, simple time of day |
| `TIMESTAMP(p)` | Yes, `p` digits (0 to 9, default 6) | No | Event times from a single zone |
| `TIMESTAMP(p) WITH TIME ZONE` | Yes | Yes, as an offset or region | Data from several zones |
| `TIMESTAMP(p) WITH LOCAL TIME ZONE` | Yes | Normalized to the database zone, shown in the session zone | Values shown in each user's own zone |
The functions are TO_TIMESTAMP for plain timestamps and TO_TIMESTAMP_TZ for timestamps with a time zone. TO_CHAR formats all of these types.
Parsing and formatting fractional seconds
The FF element stands for fractional seconds. FF alone uses the precision of the target type. FF1 to FF9 fix the number of digits.
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 ts_text
FROM dual;The result is 2026-11-28 14:30:15.123. TO_CHAR cuts the extra digits and does not round them. The input string may have fewer digits than the model asks for. TO_TIMESTAMP('...15.12', '...FF6') reads .12 as 120000 microseconds.
The . before FF is a literal character in the model. Oracle matches it against the input. If your source data uses a comma as the decimal mark, write ,FF6 in the model.
FF is only for timestamps
FF is rejected when the value is a DATE, and TO_DATE does not accept it in its format model. Both of these fail with a format error (ORA-01821, "date format not recognized"):
SELECT TO_DATE('2026-11-28 14:30:15.123', 'YYYY-MM-DD HH24:MI:SS.FF3') FROM dual;
SELECT TO_CHAR(SYSDATE, 'HH24:MI:SS.FF3') FROM dual;If a string has fractional seconds, parse it with TO_TIMESTAMP. If you need a DATE afterward, cast the result.
SELECT CAST(TO_TIMESTAMP('2026-11-28 14:30:15.789', 'YYYY-MM-DD HH24:MI:SS.FF3') AS DATE) AS as_date
FROM dual;CAST ... AS DATE drops the fraction without rounding it, so the value is 28-NOV-26 14:30:15 when shown in the default format. Use TO_CHAR(as_date, 'YYYY-MM-DD HH24:MI:SS') to see all of it.
Time zones
TO_TIMESTAMP_TZ reads an offset or a region name. The relevant model elements are:
TZHandTZMfor the hour and minute parts of an offset such as+01:00.TZRfor a region name such asEurope/Paris.TZDfor an abbreviation such asCET, which can be daylight-saving-aware.
SELECT TO_CHAR(
TO_TIMESTAMP_TZ('2026-11-28 14:30:15 +01:00', 'YYYY-MM-DD HH24:MI:SS TZH:TZM')
AT TIME ZONE 'UTC',
'YYYY-MM-DD HH24:MI:SS TZH:TZM') AS utc_text
FROM dual;This returns 2026-11-28 13:30:15 +00:00. The AT TIME ZONE clause changes the zone of a value and keeps the same instant. It does not alter the clock time as text without also changing the offset.
To attach a zone to a plain timestamp, use FROM_TZ. This does not shift the clock reading. It only states which zone the reading belongs to.
SELECT FROM_TZ(TIMESTAMP '2026-11-28 14:30:00', 'Europe/Paris') AS with_zone
FROM dual;Which clock function to call
| Function | Returns | Zone |
|---|---|---|
| `SYSDATE` | `DATE` | Database server operating system |
| `SYSTIMESTAMP` | `TIMESTAMP WITH TIME ZONE` | Database server operating system |
| `CURRENT_DATE` | `DATE` | Session time zone |
| `CURRENT_TIMESTAMP` | `TIMESTAMP WITH TIME ZONE` | Session time zone |
Choose the session-based functions when the result must match what the connected user sees. Choose the server-based functions when you want one clock for all sessions.
Writing ISO 8601 strings
A common requirement is a string like 2026-11-28T14:30:15.123Z. The T and Z are not model elements, so put them in double quotes. Oracle then copies them as literal text.
SELECT TO_CHAR(SYSTIMESTAMP AT TIME ZONE 'UTC',
'YYYY-MM-DD"T"HH24:MI:SS.FF3"Z"') AS iso_utc
FROM dual;The AT TIME ZONE 'UTC' part is what makes the trailing Z correct. A quoted "Z" is only text. If you format a value that is still in another zone, the string claims UTC but the clock reading is wrong.
Implicit conversion and the NLS timestamp formats
Earlier chapters covered the risk of leaning on session defaults. Timestamps have their own defaults:
NLS_TIMESTAMP_FORMATforTIMESTAMPandTIMESTAMP WITH LOCAL TIME ZONE.NLS_TIMESTAMP_TZ_FORMATforTIMESTAMP WITH TIME ZONE.
These are separate from NLS_DATE_FORMAT. A script can behave correctly for dates and still break for timestamps, because changing one does not change the other. An insert like the one below depends on NLS_TIMESTAMP_FORMAT in the session that runs it:
INSERT INTO events (id, created_at) VALUES (1, '28-NOV-26 02.30.15.123456 PM');The literal form is portable. It does not depend on any session setting:
INSERT INTO events (id, created_at) VALUES (1, TIMESTAMP '2026-11-28 14:30:15.123456');For values built from variables, use TO_TIMESTAMP with an explicit model, as you did with TO_DATE.
Arithmetic and what it returns
The result type of a subtraction depends on the operand types.
SELECT TO_TIMESTAMP('2026-11-28 14:30:00', 'YYYY-MM-DD HH24:MI:SS')
- TO_TIMESTAMP('2026-11-28 08:15:30', 'YYYY-MM-DD HH24:MI:SS') AS elapsed
FROM dual;The result is an INTERVAL DAY TO SECOND, shown as +000000000 06:14:30.000000000. Subtracting two DATE values gives a plain number of days, such as 0.26006944.... Use EXTRACT(HOUR FROM elapsed) and similar calls to read the parts of an interval.
If one operand is a DATE and the other a timestamp, Oracle converts the DATE to a timestamp, and the result is an interval. That is usually what you want, but check it when a query mixes column types.
Timestamps in WHERE clauses
The index rule from the previous chapter still holds. A function on the column hides it from an ordinary index. This is common with timestamp columns, because people want "everything that happened on one day".
-- Avoid: TRUNC wraps the column, so a normal index on created_at is not used
SELECT * FROM events WHERE TRUNC(created_at) = DATE '2026-11-28';
-- Prefer: a half-open range on the bare column
SELECT * FROM events
WHERE created_at >= TIMESTAMP '2026-11-28 00:00:00'
AND created_at < TIMESTAMP '2026-11-29 00:00:00';The half-open range (>= start, < next start) matters. A condition like created_at <= TIMESTAMP '2026-11-28 23:59:59' misses rows with fractional seconds after 23:59:59.000000. The < form needs no guess about the last instant of the day.
If you must filter on an expression, you can create a function-based index on that same expression, such as TRUNC(created_at). The predicate then has to use exactly that expression.
Common errors with timestamps
- ORA-01821, date format not recognized. You used
FFwithDATEorTO_DATE, or you misspelled an element. Switch toTO_TIMESTAMP. - ORA-01830, date format picture ends before converting entire input string. The input has more text than the model, often fractional seconds with no
FFin the model. - ORA-01843, not a valid month. Same cause as with dates. The model's order of day and month does not match the string.
- ORA-01857, not a valid time zone. The
TZRvalue is not a region name Oracle knows. Check spelling and use the full region name. - A shifted result with no error. The string was parsed as
TIMESTAMP, then compared with aTIMESTAMP WITH TIME ZONE. Oracle gave the plain value the session zone. State the zone yourself withFROM_TZorTO_TIMESTAMP_TZ.
The DEFAULT ... ON CONVERSION ERROR clause from the safe-conversion chapter also works with TO_TIMESTAMP and TO_TIMESTAMP_TZ, and VALIDATE_CONVERSION accepts TIMESTAMP as a target type:
SELECT TO_TIMESTAMP('not a time' DEFAULT NULL ON CONVERSION ERROR,
'YYYY-MM-DD HH24:MI:SS.FF3') AS ts,
VALIDATE_CONVERSION('2026-11-28 14:30:15.123' AS TIMESTAMP,
'YYYY-MM-DD HH24:MI:SS.FF3') AS is_valid
FROM dual;Here ts is NULL and is_valid is 1. The second value shows that the validation call checks the string it is given, and the first column shows the fallback for a different, bad string.
Practical rules
- Parse text that has fractional seconds with
TO_TIMESTAMP, neverTO_DATE. - Store a zone with the value when data comes from more than one place.
- Write timestamp literals as
TIMESTAMP '...'or with an explicit model. Do not rely onNLS_TIMESTAMP_FORMAT. - Filter timestamp columns with half-open ranges on the bare column.
- Quote literal characters such as
TandZin a format model.
Exercise
A table logins has a TIMESTAMP column login_at, and an index on it. Write a query that returns all logins on 2026-12-01, without hiding the column from the index and without missing rows that have fractional seconds.
Show answer
SELECT * FROM logins WHERE loginat >= TIMESTAMP '2026-12-01 00:00:00' AND loginat < TIMESTAMP '2026-12-02 00:00:00'; The column is bare, and the upper bound is exclusive, so a row at 23:59:59.999999 is included.
Exercise
The string '2026-11-28 09:05:07.250 -05:00' comes from a client in New York. Write a query that parses it and prints it in UTC as 'YYYY-MM-DD HH24:MI:SS.FF3'.
Show answer
SELECT TOCHAR(TOTIMESTAMPTZ('2026-11-28 09:05:07.250 -05:00', 'YYYY-MM-DD HH24:MI:SS.FF3 TZH:TZM') AT TIME ZONE 'UTC', 'YYYY-MM-DD HH24:MI:SS.FF3') AS utctext FROM dual; The result is 2026-11-28 14:05:07.250, because -05:00 is five hours behind UTC.