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

2021-12-06 00:00:00 sql date between oracle sysdate

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

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

我在阅读其他问题后写了一个 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

但它只返回空值.sysdate01/05/2014 的当前日期,日期格式为DD/MM/YYYY.

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

我写的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'); 

推荐答案

您不需要将 to_date() 应用到 sysdate.它已经在那里了:

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

如果你关心日期上的时间分量,那么使用trunc():

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

相关文章