Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if current date is between two dates Oracle SQL

I would like to select 1 if current date falls between 2 dates through Oracle SQL.

I wrote an SQL after reading through other questions.

https://stackoverflow.com/questions/2369222/oracle-date-between-query

https://stackoverflow.com/questions/2399753/select-from-table-by-knowing-only-date-without-time-oracle

But it returned only null. sysdate is the current date that is 01/05/2014 in date format DD/MM/YYYY.

The SQL I wrote is:

select 1 from dual  WHERE to_date(sysdate,'DD/MM/YYYY')  BETWEEN TO_DATE('28/02/2014', 'DD/MM/YYYY') AND TO_DATE('20/06/2014', 'DD/MM/YYYY'); 

and

select 1 from dual  WHERE to_date(sysdate,'DD/MM/YYYY') >= TO_DATE('28/02/2014', 'DD/MM/YYYY')  AND to_date(sysdate,'DD/MM/YYYY') < TO_DATE('20/06/2014', 'DD/MM/YYYY');  
like image 883
Avinesh Kumar Avatar asked Apr 30 '14 21:04

Avinesh Kumar


People also ask

How do I get the current date between two dates in SQL?

Use Date >= '2011-02-25T00:00:00' and Date < '2011-02-28T00:00:00' instead; there's no downside with this one.

Is between in Oracle inclusive?

The Oracle BETWEEN condition will return the records where expression is within the range of value1 and value2 (inclusive).

How do I get the current date in Oracle?

SYSDATE returns the current date and time set for the operating system on which the database resides. The datatype of the returned value is DATE , and the format returned depends on the value of the NLS_DATE_FORMAT initialization parameter. The function requires no arguments.


1 Answers

You don't need to apply to_date() to sysdate. It is already there:

select 1 from dual  WHERE sysdate BETWEEN TO_DATE('28/02/2014', 'DD/MM/YYYY') AND TO_DATE('20/06/2014', 'DD/MM/YYYY'); 

If you are concerned about the time component on the date, then use trunc():

select 1 from dual  WHERE trunc(sysdate) BETWEEN TO_DATE('28/02/2014', 'DD/MM/YYYY') AND                              TO_DATE('20/06/2014', 'DD/MM/YYYY'); 
like image 190
Gordon Linoff Avatar answered Sep 20 '22 23:09

Gordon Linoff