我想在PostgreSQL 9.3.5中以日期爲參數實現一個函數。
它應該涵蓋以下所有條件並相應地返回結果。作爲參數可以爲NULL的日期函數
select * from tablename
where sid is not null and dob >= '11-14-2014' order by dob;
-- Only Start Date
select * from tablename
where sid is not null and dob <= '11-21-2014' order by dob;
-- Only End Date
select * from tablename
where sid is not null and dob between '11-10-2014' and '11-21-2014' order by dob;
-- Both Start and End Dates are Given
select * from tablename
where sid is not null and dob is null;
-- Both Start and End Dates are Null
但預期我上面的SQL小提琴是行不通的。
我希望有一個通用的實現,因此如果明天我有更多new Date Fields
到標準搜索,然後我就可以在where condition
添加它們,而不是在IF
聲明每一行。
爲什麼你需要一個單獨的功能?你所有的上述查詢似乎都是你想要的。一個單獨的函數似乎有點太多,因爲它看起來像查詢處理它很好,更可讀。 – DrColossos 2014-11-25 10:24:03
@DrColossos我需要一個函數,因爲我試圖根據傳遞的參數(基於標準)來獲取細節,所以我想讓它們變成一個函數,以便我可以使用該函數返回結果作爲單行查詢。 – 09Q71AO534 2014-11-25 10:26:23