Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LESS THAN OR EQUAL TO IN Oracle SQL

Tags:

oracle

plsql

updated_date  = 08-Jun-2010;

I have a query like this

select * from asd whre updated_date <= todate('08-Jun-2010', 'dd-MM-yy');

but I am not getting any results. it only works if todate is 09-Jun-2010...

i.e. my equalto operator is not working properly.

why is it like that?

like image 834
Andromeda Avatar asked Jun 08 '10 11:06

Andromeda


2 Answers

Check this,

select to_date('08-Jun-2010', 'dd-MON-yyyy') from dual;

It is equal to 2010-06-08 00:00:00. Notice the time.

The updated_date has time portion. To include them, please use this query,

select * from asd where trunc(updated_date) <= to_date('08-Jun-2010', 'dd-MON-yyyy');

The default TRUNC function for date parameter will remove the time.

Refer Trunc

like image 55
Sujee Avatar answered Oct 30 '22 08:10

Sujee


In Oracle a DATE is a point in time. It always has a time component with a precision to the second. todate('08-Jun-2010', 'dd-Mon-yyyy') is in Oracle the same as todate('08-Jun-2010 00:00:00', 'dd-Mon-yyyy hh24:mi:ss'). So if you select rows up to that date you won't get any row on that day with a time component not equal to 00:00.

If you want to select all the rows up to and including 08-JUN-2010, I would suggest using:

< to_date('09-06-2010', 'dd-MM-yyyy')

or

<= to_date('08-06-2010 23:59:59', 'dd-MM-yyyy hh24:mi:ss')

Note - I corrected your date format: you need to use MON if you want to use the abbreviated month name. I would suggest using MM instead, so that you won't get error when someone changes its client settings (NLS_DATE_LANGUAGE). Also prefer the use of YYYY instead of YY.

like image 21
Vincent Malgrat Avatar answered Oct 30 '22 08:10

Vincent Malgrat