Sub topic 3
Getting the current date and time with SYSDATE
Assume you know how to write basic SELECT statements, use WHERE, and query Oracle's DUAL table. This chapter focuses on the source and meaning of the current date and time. It does not cover time zone conversion in depth.
What SYSDATE returns
SYSDATE is an Oracle function that returns the current date and time from the database server's operating-system clock. Its data type is DATE.
In Oracle, a DATE stores these parts:
- Year, month, and day
- Hour, minute, and second
Despite its name, Oracle's DATE value includes a time component. It does not store fractional seconds or a time-zone offset.
SELECT SYSDATE AS server_date_time
FROM dual;The displayed result depends on the session's NLS_DATE_FORMAT setting. For example, the same value might display as 28-NOV-26 even though it includes hours, minutes, and seconds internally.
Use TO_CHAR when the output must have a specific format:
SELECT SYSDATE AS default_display,
TO_CHAR(SYSDATE, 'YYYY-MM-DD HH24:MI:SS') AS explicit_display
FROM dual;TO_CHAR changes the value into text for display. It does not change the stored date value.
SYSDATE, SYSTIMESTAMP, and CURRENT_DATE
Oracle provides several functions for the current date and time. They differ mainly in precision and time-zone rules.
| Function | Return type | Clock or time zone used | Fractional seconds |
|---|---|---|---|
| `SYSDATE` | `DATE` | Database server's operating-system clock | No |
| `SYSTIMESTAMP` | `TIMESTAMP WITH TIME ZONE` | Database server's operating-system clock | Yes |
| `CURRENT_DATE` | `DATE` | Session time zone | No |
The following query lets you compare them:
SELECT SYSDATE AS sysdate_value,
SYSTIMESTAMP AS systimestamp_value,
CURRENT_DATE AS session_date,
TO_CHAR(SYSDATE, 'YYYY-MM-DD HH24:MI:SS') AS formatted_sysdate
FROM dual;SYSTIMESTAMP is useful when you need fractional seconds or the server's time-zone offset. CURRENT_DATE is useful when the application wants the current time in the session's time zone.
The session time zone can differ from the database server's time zone. Therefore, CURRENT_DATE and SYSDATE can show different clock times even when they are evaluated in the same query.
How SYSDATE fits into a query
You can use SYSDATE anywhere an expression is allowed. For example, this query finds rows created today:
SELECT order_id, order_date
FROM orders
WHERE order_date >= TRUNC(SYSDATE)
AND order_date < TRUNC(SYSDATE) + 1;TRUNC(SYSDATE) removes the time portion and produces midnight at the start of today. The second condition uses tomorrow's midnight as an exclusive upper bound. This pattern includes every time today, including rows created at 23:59:59.
Avoid this condition when you mean “all rows from today”:
WHERE order_date = TRUNC(SYSDATE)It matches only rows whose time is exactly midnight.
Clock location and repeatability
SYSDATE uses the database server's clock, not necessarily the clock on your laptop or application server. In a distributed system, this distinction can explain unexpected differences.
Because SYSDATE changes with time, queries that use it are not repeatable forever. For reports, tests, or batch jobs, pass a fixed reference date when possible. This makes the result easier to test and compare.
Exercise
Which function should you choose when you need the database server's current time with fractional seconds and a time-zone offset?
Show answer
Choose SYSTIMESTAMP. SYSDATE uses the server clock but returns DATE without fractional seconds or a time-zone offset.