PostgreSQL:具有多个日期参数的功能



我正在尝试创建一个具有多个参数的函数:

CREATE OR REPLACE FUNCTION select_name_and_date (
    IN f_name character,
    IN m_name character,
    IN l_name character,
    IN start_date  date,
    IN end_date  date )  
RETURNS TABLE (
start_date  date ,first_name character, middle_name character,last_name character ) AS $BODY$
BEGIN RETURN QUERY
select a.start_date, a.first_name, a.middle_name, a.last_name
FROM table1 a
where code in ('NEW', 'OLD')
and ( (a.first_name like '%' || f_name || '%' and a.middle_name like '%' || m_name || '%' and a.last_name like '%' || l_name || '%')) 
or ((a.date_applied) between start_date  and end_date  );
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;

当我尝试使用日期执行时,它显示正确的结果。

select * from select_name_and_date ('Firstname','','','2016-06-27','2016-06-28');

当我尝试删除日期的值时,它显示了:错误:类型日期的无效输入语法:"

select * from select_name_and_date ('Firstname','','','','');

当我尝试用日期的空值替换时,它显示了:检索0行。(应该有)

select * from select_name_and_date ('Firstname','','',NULL,NULL);

我想具有不取决于每个参数的参数。

between操作员不处理null s。如果您想允许它们,您将明确对待它们。例如,您可以重写适用于a.date_applied的条件的一部分,如下所示:

((a.date_applied BETWEEN start_date AND end_date) OR
 (start_date IS NULL AND a.date_applied < end_date) OR
 (end_date IS NULL AND a.date_applied >= end_date) OR
 (start_date IS NULL AND end_date IS NULL))

相关内容

  • 没有找到相关文章

最新更新