Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle date "Between" Query

I am using oracle database. i want to execute one query to check the data between two dates.

NAME               START_DATE     -------------    -------------  Small Widget       15-JAN-10 04.25.32.000000 PM       Product 1          17-JAN-10 04.31.32.000000 PM      select * from <TABLENAME> where start_date   BETWEEN '15-JAN-10' AND '17-JAN-10' 

But I dont get any results from above query. I think I have to use "like" and "%". But I dont know where to use them. Please throw some lights on this.

thanks in advance.

like image 807
Gnaniyar Zubair Avatar asked Mar 03 '10 06:03

Gnaniyar Zubair


People also ask

Is Oracle between inclusive?

The SQL BETWEEN OperatorThe BETWEEN operator is inclusive: begin and end values are included.

How does between works in Oracle?

The BETWEEN operator returns true if the value of expression is greater than or equal (>=) to low and less than or equal to high . The NOT BETWEEN operator negates the result of the BETWEEN operator. The BETWEEN operator is often used in the WHERE clause of the SELECT , DELETE , and UPDATE statement.

How can I get data between two dates in SQL?

You can use the dateadd function of SQL. This will return ID 1,2,3,4. We are doing a double Dateadd ; the first is to add a day to the current endDate , it will be 2012-03-28 00:00:00, then you subtract one second to make the end date 2012-03- 27 23:59:59.


2 Answers

Judging from your output it looks like you have defined START_DATE as a timestamp. If it were a regular date Oracle would be able to handle the implicit conversion. But as it isn't you need to explicitly cast those strings to be dates.

SQL> alter session set nls_date_format = 'dd-mon-yyyy hh24:mi:ss'   2  /  Session altered.  SQL> SQL> select * from t23   2  where start_date between '15-JAN-10' and '17-JAN-10'   3  /  no rows selected  SQL> select * from t23   2  where start_date between to_date('15-JAN-10') and to_date('17-JAN-10')   3  /  WIDGET                          START_DATE ------------------------------  ---------------------- Small Widget                    15-JAN-10 04.25.32.000      SQL>  

But we still only get one row. This is because START_DATE has a time element. If we don't specify the time component Oracle defaults it to midnight. That is fine for the from side of the BETWEEN but not for the until side:

SQL> select * from t23   2  where start_date between to_date('15-JAN-10')    3                       and to_date('17-JAN-10 23:59:59')   4  /  WIDGET                          START_DATE ------------------------------  ---------------------- Small Widget                    15-JAN-10 04.25.32.000 Product 1                       17-JAN-10 04.31.32.000  SQL> 

edit

If you cannot pass in the time component there are a couple of choices. One is to change the WHERE clause to remove the time element from the criteria:

where trunc(start_date) between to_date('15-JAN-10')                              and to_date('17-JAN-10') 

This might have an impact on performance, because it disqualifies any b-tree index on START_DATE. You would need to build a function-based index instead.

Alternatively you could add the time element to the date in your code:

where start_date between to_date('15-JAN-10')                       and to_date('17-JAN-10') + (86399/86400)  

Because of these problems many people prefer to avoid the use of between by checking for date boundaries like this:

where start_date >= to_date('15-JAN-10')  and start_date < to_date('18-JAN-10') 
like image 178
APC Avatar answered Oct 12 '22 22:10

APC


You need to convert those to actual dates instead of strings, try this:

SELECT * FROM <TABLENAME> WHERE start_date BETWEEN TO_DATE('2010-01-15','YYYY-MM-DD') AND TO_DATE('2010-01-17', 'YYYY-MM-DD'); 

Edited to deal with format as specified:

SELECT * FROM <TABLENAME> WHERE start_date BETWEEN TO_DATE('15-JAN-10','DD-MON-YY') AND TO_DATE('17-JAN-10','DD-MON-YY'); 
like image 22
Chad Birch Avatar answered Oct 12 '22 22:10

Chad Birch