I have sql something like this:
SELECT EMP_NAME, DEPT
FROM EMPLOYEE
WHERE TIME_CREATED >= TO_DATE('26/JAN/2011','dd/mon/yyyy')
-> This returns 10 rows and TIME_CREATED = '26-JAN-2011'
Now when i do this i don't get any rows back,
SELECT EMP_NAME, DEPT
FROM EMPLOYEE
WHERE TIME_CREATED = TO_DATE('26/JAN/2011','dd/mon/yyyy')
-> Took the greater than out
Any reason why?
Yes: TIME_CREATED contains a date and a time. Use
TRUNC
to strip the time:UPDATE:
As Dave Costa points out in the comment below, this will prevent Oracle from using the index of the column
TIME_CREATED
if it exists. An alternative approach without this problem is this:This is because a
DATE
column in Oracle also contains a time part. The result of theto_date()
function is a date with the time set to00:00:00
and thus it probably doesn't match any rows in the table.You should use:
You can also use the following to include the TIME portion in your query:
As other people have commented above, using TRUNC will prevent the use of indexes (if there was an index on TIME_CREATED). To avoid that problem, the query can be structured as
86399 being 1 second less than the number of seconds in a day.
You could also do: