Check if current date is between two dates Oracle SQL
332,361
Solution 1
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');
Solution 2
SELECT to_char(emp_login_date,'DD-MON-YYYY HH24:MI:SS'),A.*
FROM emp_log A
WHERE emp_login_date BETWEEN to_date(to_char('21-MAY-2015 11:50:14'),'DD-MON-YYYY HH24:MI:SS')
AND
to_date(to_char('22-MAY-2015 17:56:52'),'DD-MON-YYYY HH24:MI:SS')
ORDER BY emp_login_date
Related videos on Youtube
Author by
Avinesh Kumar
I am an Analyst Programmer at The University of the South Pacific and I manage student data at the university. I am currently doing my Masters and i am specialised in Data Security, Information Systems, Computing Science and Software Engineering.
Updated on August 22, 2020Comments
-
Avinesh Kumar over 3 years
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
But it returned only null.
sysdate
is the current date that is01/05/2014
in date formatDD/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');
-
paul about 10 yearsdo you need to
TO_DATE()
sysdate? Surely it is already a date -
Avinesh Kumar about 10 years
-
Jeffrey Kemp about 10 yearsYou don't need SQL for this - in PL/SQL you can just do something like
if sysdate between date '2014-02-28' and date '2014-06-21' then v := 1; end if;
-
Avinesh Kumar about 10 yearsthanks Jeffrey Kemp this also works fine for me.
-
-
Nathan Tuggy almost 9 yearsGenerally, answers are much more helpful if they include an explanation of what the code is intended to do, and why that solves the problem without introducing others. (This post was flagged by at least one user, presumably because they thought an answer without explanation should be deleted.)
-
anilech almost 6 years
trunc(sysdate)
in this example (dual table) is ok to handle time portion of a date, but in the real table with indexed DATE field it may prevent Oracle from using the corresponding index. I would suggest not using "between" clause but use comparison operators here:where DATE_FIELD >= TO_DATE('28/02/2014', 'DD/MM/YYYY') and DATE_FIELD < TO_DATE('20/06/2014', 'DD/MM/YYYY') + 1
-
Abhishek over 5 yearsThis works like charm. Just need to understand formatting with to_date.
-
Aba over 4 yearsAnoter way to deal with the index is from dba-oracle.com/t_sql_display_all_days_between_two_dates.htm:
select ... where trunc(ORDER_DATE) between ...
BEWARE: Using the trunc function will invalidate the index on order_date, and you may want to create a temporary function-based index ontrunc(order_date)
-
Gordon Linoff over 4 years@Aba . . . Am I missing something? There is no
order_date
in this question and no index on dual. -
Aba over 4 years@GordonLinoff I was just commenting on the issue of trunc() and indexes which anilech brought to light when, as he pointed out, when dealing with a real table.