Skip to Main Content
  • Questions
  • Default date part of TO_DATE function

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, Henk.

Asked: January 25, 2018 - 12:54 pm UTC

Last updated: January 26, 2018 - 10:37 am UTC

Version: 12.2

Viewed 10K+ times! This question is

You Asked

Hi AT-Team,

LiveSQL seems to be broken, so sorry no test case there.

We are building a system in which developers are converting strings holding time-only data to a DATE in Oracle SQL in order to build queries involving the time of day later on. Simple enough: use TO_DATE(). However I am wondering about the rules about the date part of the resulting datetime value.

E.g.
(...)
to_date('13:00:00', 'hh24:mi:ss')
(...)

This works fine.

But when I select the resulting value back:
select to_char(to_date('13:00:00', 'hh24:mi:ss'), 'yyyy-mm-dd"T"hh24:mi:ss')
from dual;


results in:
TO_CHAR(TO_DATE('13
-------------------
2018-01-01T13:00:00


So here we see the date value of '2018-01-01' is added to the time value by the to_date() call. My question is where this behavior is documented? I would like to come with some advise to the developers how to handle this use case and all I have right now is the assumption that the fist day of the current year of the database servers system date is taken but I cannot confirm this in documentation at all.

Any thoughts?

Regards and Thanks,
Henk

and Chris said...

If this is in the documentation, I can't find it ;)

But MOS note 1014079.6 discusses this. It gives the following rules

- Day missing = return 1st of the month
- Month missing = return current month

Also the following rule is true to my knowledge:

- Year missing = return current year

select to_char(sysdate, 'DD MON YYYY') today,
       to_date('FEB 2016', 'MON YYYY') no_day,
       to_date('22 2017', 'DD YYYY') no_month,
       to_date('22 MAR', 'DD MON') no_year
from   dual;

TODAY         NO_DAY                 NO_MONTH               NO_YEAR                
25 JAN 2018   01-FEB-2016 00:00:00   22-JAN-2017 00:00:00   22-MAR-2018 00:00:00   

Rating

  (3 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

An Idea

Chuck Jolley, January 25, 2018 - 3:08 pm UTC

How about defaulting in a date no one will ever confuse with a real date
e.g. to_date('19000101'||'13:00:00', 'yyyymmddhh24:mi:ss')
Chris Saxon
January 25, 2018 - 3:19 pm UTC

Good suggestion; helps make it clearer what's going on.

PS

Chuck Jolley, January 25, 2018 - 3:11 pm UTC

PS. The suggestion is for Henk's code, not Oracle. O

Documentation

Albert Nelson, January 26, 2018 - 7:34 am UTC

Refer:
https://docs.oracle.com/database/121/NLSPG/ch4datetime.htm#GUID-4D95F6B2-8F28-458A-820D-6C05F848CA23

<quote>
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.
</quote>
Chris Saxon
January 26, 2018 - 10:37 am UTC

Thanks for digging that out.

More to Explore

SQL

The Oracle documentation contains a complete SQL reference.