-3
我有一個查詢有三個提示;部門,起始日期和截止日期。必須選擇部門ID,但可以選擇選擇日期範圍。我怎樣才能使日期範圍可選?我正在考慮使用解碼功能,但不知道如何編寫,因此兩個日期提示可以留空。解碼函數
我有一個查詢有三個提示;部門,起始日期和截止日期。必須選擇部門ID,但可以選擇選擇日期範圍。我怎樣才能使日期範圍可選?我正在考慮使用解碼功能,但不知道如何編寫,因此兩個日期提示可以留空。解碼函數
如果使用存儲過程,你可以做這樣的事情在你的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)
這要看您的具體情況。有些查詢適用於第一個到第二個。
假設未指定的日期輸入給人的印象是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
;
需要在你的數據的性質更多的信息,以考慮部門作爲輸入...請問表存儲每個部門的多個日期?
沒有足夠的信息。您應該包含示例數據和可能的預期結果。 – Parado