检查当前日期是否介于两个Oracle SQL日期之间

ia2d9nvy  于 2022-11-22  发布在  Oracle
关注(0)|答案(4)|浏览(141)

如果当前日期福尔斯Oracle SQL的两个日期之间,我希望选择1
我在阅读完其他问题后编写了一个SQL。
https://stackoverflow.com/questions/2369222/oracle-date-between-query
https://stackoverflow.com/questions/2399753/select-from-table-by-knowing-only-date-without-time-oracle
但它只返回null。sysdate是当前日期,其日期格式为DD/MM/YYYY,即01/05/2014
我写的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');

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

camsedfj1#

您不需要将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');

如果您关心日期的时间部分,则使用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');
2ul0zpep

2ul0zpep2#

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
axr492tv

axr492tv3#

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

pxq42qpu4#

在oracle下面使用、
select * from table_name其中日期列名介于截止日期('1-OCT-22')和('31-OCT-22')之间
PS:替换表名和日期列名并相应地格式化

相关问题