Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle DateTime in Where Clause?

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?

like image 710
sanjeev40084 Avatar asked Jul 19 '11 15:07

sanjeev40084


People also ask

Does Oracle TIMESTAMP have timezone?

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.

How do I insert date in YYYY MM DD in Oracle?

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.

Can we store TIMESTAMP in date datatype in Oracle?

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.


1 Answers

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 
like image 189
Daniel Hilgarth Avatar answered Sep 20 '22 15:09

Daniel Hilgarth