如何在SQL中检查日期是否在2个日期之间?

时间:2018-07-25 11:27:58

标签: sql oracle

我想基于2个日期以内的Date检索行,或者
   它将大于日期或小于日期

where id =5 
 AND ( :fromDate is  null or  trunc ( :fromDate ) >= trun(TABLE.DEPOSIT_DATE ))
 AND ( :toDate is  null or    trunc ( :toDate )<= trunc (TABLE.DEPOSIT_DATE ))

例如,当我以这种格式(7/25/2018 1:21:55 PM)将参数传递给:fromDate和:ToDate时

查询返回0行

3 个答案:

答案 0 :(得分:0)

尝试使用“ BETWEEN”运算符,如下例所示

SELECT * FROM employee where joining_date BETWEEN '2006-02-14 04:34:33' AND '2016-02-15 04:34:12';

答案 1 :(得分:0)

如果您使用的是SQL Server 然后先设置日期格式 像:-设置daqteformat dmy 您可以在之间使用的日期条件 ,例如:“ 01/01/2018”和“ 31/01/2018”之间的输入日期

答案 2 :(得分:0)

为什么您不能在之间使用?使用plsql块语句检查下面的示例以使用与您的示例相同的绑定变量

SET serveroutput ON size 2000
/
declare
  T_DATE date;
  F_DATE DATE;
  C_ID   number(2);
begin
  T_DATE := to_date('01/07/2018','dd/mm/yyyy');
  F_DATE := to_date('01/06/2018','dd/mm/yyyy');

  delete from  ex_employee;

  insert into ex_employee (id, X_NAME, T_ADDED) values (1, 'aa', sysdate);
  insert into ex_employee
    (id, X_NAME, T_ADDED)
  values
    (2, 'bb', sysdate - 1);
  insert into ex_employee
    (id, X_NAME, T_ADDED)
  values
    (3, 'cc', to_date('20/06/2018','dd/mm/yyyy'));
  commit;

  select id
    into C_ID
    from ex_employee
      where T_ADDED between F_DATE and T_DATE or F_DATE is null or T_DATE is null;
dbms_output.put_line(c_id);
end;
/




PL/SQL procedure successfully completed
  3