I was about to do some date/time calculations as I got an unexpected result with the to_date-function.
select to_date('12:13', 'hh24:mi'), sysdate from dual
2020-09-01 12:13:00 2020-09-28 08:59:01
As I did the to_date-conversion, based only on the hour and minute pattern, the date is set to the first of the current month (the format is yyyy-mm-dd hh24:mi:ss). I was not expecting that.
The same applies when converting to timestamps:
select
to_timestamp_tz('12:13', 'hh24:mi'),
to_timestamp('12:13', 'hh24:mi')
from dual;
2020-09-01 12:13:00.000000000 2020-09-01 12:13:00.000000000
So – if you’re, like me, planning on doing further calculations, be sure you know what your time or actually date, that is, is converted to.
(tested on Oracle 18c and 19c (19.0))
Reply to “A small unexpected to_date oddity”
The oddity is in fact documented. For example (from the documentation for version 10.1):
“If you specify a date value without a time component, then the default time is midnight. If you specify a date value without a date, then the default date is the first day of the current month.”
Found here: https://docs.oracle.com/cd/B14117_01/server.101/b10749/ch4datetime.htm#i1006006
LikeLike
You’re absolutely right in pointing that out. Thanks!
LikeLike