页面输入2个字段 生效和到期日期 数据库也有这2个字段 如何判断页面输入的时间范围不在数据库时间范围内
并且不能有交集,如页面输入2012-02-02,2012-08-08 数据库的范围是2012-01-01,2012-09-09
返回false 用sql和java 都可以 望解答 谢谢
另页面的时间范围传入后台,到期日期必定大于生效日期,后台数据库同样如:数据库范围2012-01-01和2012-09-09用户页面输入2012-01-02和2012-10-10 返回false
            2012-10-10和2012-12-10 返回true
            2012-03-03和2012-08-08 返回fasle

解决方案 »

  1.   

    这个很简单,只要用户输入的两个日期都不在数据库日期范围内,并且数据库开始与结束日期其中一个(不用判断两个)不在用户返回的范围内,就是true,其他条件全false就可以了
      

  2.   

    第一句话理解了 输入的2个日期分别不在数据库的范围呢 
    后面不太明白能不能写下SQL感谢
      

  3.   


    SQL> declare
      2   v_usedstart1 date := to_date('2012-01-02','yyyy-mm-dd');
      3   v_usedend1 date := to_date('2012-10-10','yyyy-mm-dd');
      4   v_usedstart2 date := to_date('2012-10-10','yyyy-mm-dd');
      5   v_usedend2 date := to_date('2012-12-10','yyyy-mm-dd');
      6   v_usedstart3 date := to_date('2012-12-10','yyyy-mm-dd');
      7   v_usedend3 date := to_date('2012-08-08 ','yyyy-mm-dd');
      8   v_databasestart date := to_date('2012-01-01','yyyy-mm-dd');
      9   v_databaseend date := to_date('2012-09-09','yyyy-mm-dd');
     10  begin
     11   if (v_usedstart1 not between v_databasestart and v_databaseend) and
     12   (v_usedend1 not between v_databasestart and v_databaseend) and
     13    (v_databasestart not between v_usedstart1 and v_usedend1) then
     14   dbms_output.put_line('第一种情况:'||'true');
     15   else
     16   dbms_output.put_line('第一种情况:'||'false');
     17   end if;
     18   if (v_usedstart2 not between v_databasestart and v_databaseend) and
     19   (v_usedend2 not between v_databasestart and v_databaseend) and
     20    (v_databasestart not between v_usedstart2 and v_usedend2) then
     21   dbms_output.put_line('第二种情况:'||'true');
     22   else
     23   dbms_output.put_line('第二种情况:'||'false');
     24   end if;
     25   if (v_usedstart3 not between v_databasestart and v_databaseend) and
     26   (v_usedend3 not between v_databasestart and v_databaseend) and
     27    (v_databasestart not between v_usedstart3 and v_usedend3) then
     28   dbms_output.put_line('第三种情况:'||'true');
     29   else
     30   dbms_output.put_line('第三种情况:'||'false');
     31   end if;
     32  end;
     33  /
    第一种情况:false                                                                
    第二种情况:true                                                                 
    第三种情况:false                                                                PL/SQL 过程已成功完成。
      

  4.   

    大哥能不能写个select - -啊我逻辑有点混乱 我这个写的校验不对select count(*) from tableName t 
    where t.startdate < to_date('2012-08-19','yyyy-mm-dd')
    and t.enddate >  to_date('2012-09-20','yyyy-mm-dd')
    数据库2个日期是start 2012-08-18 和 end 2012-08-23
    上面句sql返回无记录 应该是有记录 当中时间有范围是同样的 
      

  5.   

    按你这个sql,肯定返回 0 了。感觉你的逻辑是: 
    if (user_startdata < db_startdata or user_enddate > db_enddate or user_startdate > user_enddate ) then
    false
    else
    true
    end if
      

  6.   

    晕,和这个逻辑不一样的,差的远了
    select count(*) from tableName t 
    where (to_date('2012-08-19','yyyy-mm-dd') not between t.startdate and t.enddate)
    and (to_date('2012-09-20','yyyy-mm-dd') not between t.startdate and t.enddate)
    and (t.startdate not between to_date('2012-08-19','yyyy-mm-dd') and to_date('2012-09-20','yyyy-mm-dd'));
      

  7.   

    昨天没看清题。
    似乎是要更新有效期,新的有效期不能和旧的有效期重复,下面这个也可以吧(前提是数据库里面的enddate>startdate):
    select × from tablename t
    where to_date('2012-08-19','yyyy-mm-dd')< to_date('2012-09-20','yyyy-mm-dd')
    and (to_date('2012-08-19','yyyy-mm-dd') > t.enddate 
       or to_date('2012-09-20','yyyy-mm-dd') < t.startdate)如果新的有效期不能比旧的有效期旧,可以把最后一行的 or 去掉。