页面输入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
并且不能有交集,如页面输入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
后面不太明白能不能写下SQL感谢
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 过程已成功完成。
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返回无记录 应该是有记录 当中时间有范围是同样的
if (user_startdata < db_startdata or user_enddate > db_enddate or user_startdate > user_enddate ) then
false
else
true
end if
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'));
似乎是要更新有效期,新的有效期不能和旧的有效期重复,下面这个也可以吧(前提是数据库里面的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 去掉。