检查当前日期是否在Oracle SQL的两个日期之间

时间:2021-09-19 08:36:11

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

如果当前日期通过Oracle SQL在2个日期之间,我想选择1。

I wrote an SQL after reading through other questions.

我在阅读完其他问题后写了一篇SQL。

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

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

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

https://*.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.

但它只返回null。 sysdate是日期格式为DD / MM / YYYY的当前日期是01/05/2014。

The SQL I wrote is:

我写的SQL是:

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'); 

3 个解决方案

#1


40  

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

您不需要将to_date()应用于sysdate。它已经存在:

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():

如果您担心日期的时间组件,那么使用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');

#2


0  

TSQL: Dates- need to look for gaps in dates between Two Date

TSQL:日期 - 需要查找两个日期之间的日期差距

    select
    distinct
    e1.enddate,
    e3.startdate,
    DATEDIFF(DAY,e1.enddate,e3.startdate)-1 as [Datediff]
    from #temp e1 
       join #temp e3 on e1.enddate < e3.startdate          
           /* Finds the next start Time */
    and e3.startdate = (select min(startdate) from #temp e5
    where e5.startdate > e1.enddate)
    and not exists (select *  /* Eliminates e1 rows if it is overlapped */
    from #temp e5 
    where e5.startdate < e1.enddate and e5.enddate > e1.enddate);

#3


-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

#1


40  

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

您不需要将to_date()应用于sysdate。它已经存在:

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():

如果您担心日期的时间组件,那么使用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');

#2


0  

TSQL: Dates- need to look for gaps in dates between Two Date

TSQL:日期 - 需要查找两个日期之间的日期差距

    select
    distinct
    e1.enddate,
    e3.startdate,
    DATEDIFF(DAY,e1.enddate,e3.startdate)-1 as [Datediff]
    from #temp e1 
       join #temp e3 on e1.enddate < e3.startdate          
           /* Finds the next start Time */
    and e3.startdate = (select min(startdate) from #temp e5
    where e5.startdate > e1.enddate)
    and not exists (select *  /* Eliminates e1 rows if it is overlapped */
    from #temp e5 
    where e5.startdate < e1.enddate and e5.enddate > e1.enddate);

#3


-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