Internals of SYSDATE and Date Comparisons in Oracle
1. What is SYSDATE?
SYSDATE returns the current date and time from the system clock of the database server, not the client.
Data Type:
-
Oracle
DATEtype -
Stored internally as a 7-byte binary:
Example:
Let's say SYSDATE = 13-MAY-25 14:37:29. Internally it's stored as:
2. Key Concepts
A. DATE Data Type Always Has Time
Even if you see:
It is actually:
B. Display vs Storage
Oracle may display a date as DD-MON-YY depending on NLS_DATE_FORMAT, but it still contains time unless explicitly truncated.
3. Comparisons: Real Examples
Let’s create a test table:
Insert data with different time portions:
Now try:
This only matches row 2, not row 1, because it assumes time 00:00:00.
4. Safe Comparisons
A. Match just the date:
-
TRUNCremoves time. -
Internally, Oracle compares only the date part, ignoring hours, minutes, seconds.
B. Match all values from today:
-
This includes
23:59:59on the same day.
5. Why SYSDATE + 1 May Show Only Date
You might see:
Why?
It’s because:
-
SYSDATE + 1adds exactly 1 day (24 hours). -
If NLS format only shows date (
DD-MON-YY), time is hidden.
But the internal structure is still:
6. Execution and Optimizer Notes
-
Oracle compares
DATEfields byte-by-byte. -
When comparing
SYSDATE = column, it checks all 7 bytes. -
If you use
TRUNC(column) = TRUNC(SYSDATE), Oracle can’t use index oncolumnunless it's a function-based index onTRUNC(column).
7. Function-Based Index for Performance
To avoid full scan:
Now:
can use the index.
8. Common Mistakes
| Mistake | What Happens |
|---|---|
WHERE date_col = SYSDATE | Fails unless exact time matches |
WHERE date_col = TO_DATE('13-MAY-25') | Assumes time = 00:00:00 |
Using TRUNC() on indexed column | Index may be ignored unless function-based |

Post a Comment
Post a Comment