I have two tables where I was querying on a date column in each table.
In one case need to use the trunc() function on the date field to get values back, on the other I do not.
That is this works on table 1:
SELECT to_char( datecol1 ,'mm/dd/yyyy hh:mm:ss')
FROM table1 where datecol1 =to_date('10/07/2010', 'mm/dd/yyyy');
But on table 2 the above syntax did not work and I needed the trunc(), such as:
SELECT to_char( datecol2 ,'mm/dd/yyyy hh:mm:ss')
FROM table2 where trunc(datecol2) =to_date('10/07/2010', 'mm/dd/yyyy');
Three things to note:
- in querying table1 with to_char(datecol1 ,’mm/dd/yyyy hh:mm:ss’) it looks as if all the times are between 12:00 and 12:10, but values were inserted throughout the day
- when inserting records into table1 I just insert mm/dd/yyyy, no time
- when inserting records into table2 I inserted with the time
So can someone explain:
- why the truncate is not needed on table1 but on table2?
- why all values in table1 are between 12:00 and 12:10?
In table1 you have no ‘time-of-day’ component to the data, so a date should match – which is what you observed. But, you used
mmfor the formatting of the minute part of the time – butmmis month, not minute (mi). This is why you see times other than 12:00, and why they only range up to around 12:10 (you only have data for this year perhaps?)In table2, as you have a ‘time-of-day’ component to the data, you need to truncate that away in order to match a date-only value, which is what the
to_date()function returns, given the format you have used.