Meantime, DATE
and TIMESTAMP(0)
datatypes are not the same in Oracle
Oracle differs from many other RDBMS in that its DATE
data type ALWAYS contains both a date and a time component. Its implementation predates the ANSI standard.
In Oracle, if you have the table:
CREATE TABLE table_name (ts TIMESTAMP(0), dt DATE);
and insert the data:
INSERT INTO table_name (ts, dt) VALUES (SYSDATE, SYSDATE);
Then you can look at the binary data being stored using the DUMP
function:
SELECT DUMP(ts) AS dump_ts,
DUMP(dt) AS dump_dt
FROM table_name;
Which outputs:
DUMP_TS |
DUMP_DT |
Typ=180 Len=7: 120,122,3,25,15,13,37 |
Typ=12 Len=7: 120,122,3,25,15,13,37 |
Then you can see that they are both stored as 7-byte binary values:
120
= Century + 100
122
= Year-of-century + 100
3
= Month
25
= Day
15
= Hour + 1
13
= Minutes + 1
37
= Seconds + 1
And the binary values are identical (the only difference is in the meta-data Typ
where 180 = TIMESTAMP
and 12 = DATE
).
Effectively, they are stored identically.
db<>fiddle here
However
The side-effects of a TIMESTAMP
vs. a DATE
data type in Oracle may lead to different effects.
When you subtract a TIMESTAMP
and either a TIMESTAMP
or a DATE
then the return value is an INTERVAL DAY TO SECOND
data type.
When you subtract a DATE
and a DATE
then the default return value is a NUMBER
representing the number of days difference.
When you display a TIMESTAMP
then the client application you are using may default to using the NLS_TIMESTAMP_FORMAT
session parameter to format the timestamp as a string and the default for this parameter will typically show date, time and fractional seconds.
When you display a DATE
then the client application you are using may default to using the NLS_DATE_FORMAT
session parameter to format the date as a string and the default for this parameter will show date but not time (and there will never be any fractional seconds to show). Just because the client application may chose not to show the time component does not mean that the time component does not exist.
If you set the session parameters using:
ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS';
ALTER SESSION SET NLS_TIMESTAMP_FORMAT = 'YYYY-MM-DD HH24:MI:SS';
Then, provided your client application is using those parameters to format them, they will display identically.
The problem you are seeing with the difference in Oracle is due to these side effects.