I was trying to select some data from my table using the following query:
select * from table1 where column1 = to_date('14-05-14','yy-mm-dd');
Where the column data type is
DATE. I observed that, the above query won't return anything unless we modified it as,
select * from table1 where trunc(column1) = to_date('14-05-14','yy-mm-dd');
even though there are records available.
I checked the documentation for
TRUNC.Can anyone please explain why this happens?
As per the valuable comments I think some
time values may also associated with the DATE. But I cannot view/edit that
time. How can I ensure there are
time values associated.
preguntado el 28 de mayo de 14 a las 12:05
TRUNC are different. See the below example.
SQL> ALTER SESSION SET nls_date_format = 'dd/mm/yyyy hh24:mi:ss'; Session altered. SQL> SELECT TO_DATE(SYSDATE) FROM DUAL; TO_DATE(SYSDATE) ------------------- 28/05/2014 16:03:25 SQL> SELECT TRUNC(SYSDATE) FROM DUAL; TRUNC(SYSDATE) ------------------- 28/05/2014 00:00:00
In Your first query
to_date('14-05-14','yy-mm-dd') is comparing with the date field
column1 in your table which has time values also. Whereas in Your 2nd query You are truncating the time part from table data and from Your query, that's why it's matching.
The DATE datatype stores the year (including the century), the month, the day, the hours, the minutes, and the seconds (after midnight).
TRUNC function will truncate the date to the day value, so that any hours, minutes, or seconds will be truncated off.
For more info please look at these below links