我正在尝试使用多个参数创建一个函数,如下所示:
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');
当我尝试删除date的值时,它显示: 错误:类型为date的输入语法无效:“”
select * from select_name_and_date ('Firstname','','','','');
当我尝试用日期的NULL值替换时,它显示:检索到0行。 (当它应该有的时候)
select * from select_name_and_date ('Firstname','','',NULL,NULL);
我希望参数不依赖于每个参数。
答案 0 :(得分:3)
between
运算符无法处理null
。如果你想允许它们,你将明确地对待它们。例如,您可以重写适用于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))