Simple query with date type requires explicit conversion to_date of date My question is, I have a table which contains a column called myDateColumn (for example) which is of the type date:

I can confirm desc myTable contains this line:

myDateColumn  NOT NULL     DATE   


However, when I try to select all the data that is between certain dates, for example:

select * from myTable 
where myDateColumn 
between to_date('13-FEB-11', 'DD-MON-YY') AND TO_DATE('15-FEB-11', 'DD-MON-YY');


I get null results no matter what, even though I am sure some data exists within this specific date range.

The fix for it I discovered was to explicitly convert myDateColumn to a date:

select myDateColumn from myTable 
where to_date(myDateColumn) 
between to_date('13-FEB-11', 'DD-MON-YY') AND TO_DATE('15-FEB-11', 'DD-MON-YY');


which returns the correct results, but it is already a date type!  Why do I need to explicitly convert a date column to date before doing the between logical comparison?

The reason why I ask is because this select/ where will be part of a stored procedure selecting data within a specific date range to be run on a big table partitioned by date, with more than ten million rows, and if I can avoid explictly converting each myDateColumn of each row, then I might be able to save some query time, in theory.

Or is there a more correct way to run this comparison to select data within a date range?

Thank you.

Update:  Leigh suggested in the answer to diagnose the table for something inconsistent with his query.  The result of his query was:

myDateColumn    TO_DATE(myDateColumn)   TO_CHAR(myDateColumn,'DD-MON-YYHH.MI.SSPM') TO_CHAR(myDateColumn,'YYYY')  
14-Feb-11   14-Feb-11   14-FEB-11 12.00.00 AM   0011
14-Feb-11   14-Feb-11   14-FEB-11 12.00.00 AM   0011
14-Feb-11   14-Feb-11   14-FEB-11 12.00.00 AM   0011
14-Feb-11   14-Feb-11   14-FEB-11 12.00.00 AM   0011
14-Feb-11   14-Feb-11   14-FEB-11 12.00.00 AM   0011


Indicating that the reason the between clause was not working, is because none of the data would fall in the query interval, since the year was 0011, during the Roman Empire :)

Thanks guys.
All dates in Oracle have a time component.

I'm pretty sure you want all rows between the 13th and the 15th included. However, when you write:

BETWEEN to_date('13-02-2011', 'DD-MM-YYYY') AND TO_DATE('15-02-2011', 'DD-MM-YYYY')


you select only all date up to the 15-02-2011 at midnight. All rows later that day won't be selected.

What is happening is that when you use TO_DATE on a date, it will be converted to VARCHAR2 and then converted back to a date. The net effect will be a truncation to your date format as shown:

SQL&gt; alter session set nls_date_format='dd/mm/yyyy';

Session altered.

SQL&gt; select to_char(to_date(sysdate), 'dd/mm/yyyy hh24:mi:ss') trunc,
  2         to_char(sysdate, 'dd/mm/yyyy hh24:mi:ss') not_trunc
  3    from dual;

TRUNC               NOT_TRUNC
------------------- -------------------
23/10/2012 00:00:00 23/10/2012 15:15:32


So your two clauses are not equivalent at all. The second one selects the whole day feb, 15th whereas the first one only selects the first second of the day.

I suggest you use the following constructions when writing date range:

WHERE dat &gt;= to_date('13-02-2011', 'DD-MM-YYYY')
  AND dat &lt; to_date('16-02-2011', 'DD-MM-YYYY')


Also don't use MON or DAY format in your code since they are dependent upon language. Don't use YY since this can be confusing (ever heard of the Y2k bug?). Both formats are ok to display information to your users of course.