2013-08-01 34 views
-3

我有一个查询有三个提示;部门,起始日期和截止日期。必须选择部门ID,但可以选择选择日期范围。我怎样才能使日期范围可选?我正在考虑使用解码功能,但不知道如何编写,因此两个日期提示可以留空。解码函数

+0

没有足够的信息。您应该包含示例数据和可能的预期结果。 – Parado

回答

0

如果使用存储过程,你可以做这样的事情在你的SELECT语句:

select * 
from table 
where (field > inDateStart and field < inDateEnd) or 
     (inDateStart is null and inDateEnd is null) 

或使用合并

select * 
from table 
where (field => coalesce(inDateStart,field) and 
     field <= coalesce(inDateEnd,field) 

这要看您的具体情况。有些查询适用于第一个到第二个。

0

假设未指定的日期输入给人的印象是NULL,你可以做到这一点小窍门:

with 
TheTable as 
(select 1 dept, sysdate dt from dual 
union 
select 2 dept, sysdate-63 dt from dual 
union 
select 3 dept, sysdate-95 dt from dual 
) 
select * 
from thetable 
where coalesce(:DateFrom,dt) <= dt 
and coalesce(:DateTo,dt) >= dt 
; 

需要在你的数据的性质更多的信息,以考虑部门作为输入...请问表存储每个部门的多个日期?