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?
Some explanations about the Oracle TIMESTAMP data types: TIMESTAMP : Does not store any timezone information. If you enter a timestamp with time zone then the time zone information is simply truncated and lost.
The TO_DATE function allows you to define the format of the date/time value. For example, we could insert the '3-may-03 21:02:44' value as follows: insert into table_name (date_field) values (TO_DATE('2003/05/03 21:02:44', 'yyyy/mm/dd hh24:mi:ss')); Learn more about the TO_DATE function.
As Oracle provides DATE data type to represent date and time values. This data type has ability to store the day, month, year, century, hour, minute and seconds. Oracle introduced a new data type TIMESTAMP which is an extension of DATE data type.
Yes: TIME_CREATED contains a date and a time. Use TRUNC
to strip the time:
SELECT EMP_NAME, DEPT FROM EMPLOYEE WHERE TRUNC(TIME_CREATED) = TO_DATE('26/JAN/2011','dd/mon/yyyy')
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:
SELECT EMP_NAME, DEPT FROM EMPLOYEE WHERE TIME_CREATED >= TO_DATE('26/JAN/2011','dd/mon/yyyy') AND TIME_CREATED < TO_DATE('26/JAN/2011','dd/mon/yyyy') + 1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With