Paste this into a new question or an answer at dba.stackexchange.com:
<!-- --> > CREATE TABLE test ( > value1 text, > value2 text, > value3 text, > value4 text > ); > > INSERT INTO test VALUES > ('a',NULL,NULL,'d'), > ('a', 'b', 'c', 'd'); -- is get out as result row in GetValues_old > > SELECT * FROM test; > > <pre> > ✓ > 2 rows affected > > value1 | value2 | value3 | value4 > :----- | :----- | :----- | :----- > a | <em>null</em> | <em>null</em> | d > a | b | c | d > </pre> <!-- --> > CREATE OR REPLACE FUNCTION "GetValues_old"( > "@value1" VARCHAR(50), > "@value2" VARCHAR(50), > "@value3" VARCHAR(50), > "@value4" VARCHAR(50) > ) RETURNS SETOF test AS $$ > BEGIN > RETURN QUERY SELECT > t."value1", > t."value2", > t."value3", > t."value4" > > FROM test as t > WHERE ("@value1" IS NULL OR t."value1" = "@value1") > AND ("@value2" IS NULL OR t."value2" = "@value2") > AND ("@value3" IS NULL OR t."value3" = "@value3") > AND ("@value4" IS NULL OR t."value4" = "@value4"); > END; > $$ LANGUAGE plpgsql; > > <pre> > ✓ > </pre> <!-- --> > SELECT * FROM "GetValues_old"('a', NULL, NULL, 'd') > > <pre> > value1 | value2 | value3 | value4 > :----- | :----- | :----- | :----- > a | <em>null</em> | <em>null</em> | d > a | b | c | d > </pre> <!-- --> > CREATE OR REPLACE FUNCTION "GetValues"( > "@value1" VARCHAR(50), > "@value2" VARCHAR(50), > "@value3" VARCHAR(50), > "@value4" VARCHAR(50) > ) RETURNS SETOF test AS $$ > BEGIN > RETURN QUERY SELECT > t."value1", > t."value2", > t."value3", > t."value4" > > FROM test as t > WHERE row(t.*) IS NOT DISTINCT FROM row("@value1", "@value2", "@value3", "@value4"); > END; > $$ LANGUAGE plpgsql; > > <pre> > ✓ > </pre> <!-- --> > SELECT * FROM "GetValues"('a', NULL, NULL, 'd') > > <pre> > value1 | value2 | value3 | value4 > :----- | :----- | :----- | :----- > a | <em>null</em> | <em>null</em> | d > </pre> *db<>fiddle [here](https://dbfiddle.uk/?rdbms=postgres_9.6&fiddle=b6a866ee2e6f72660fe8cded53eca71a)*
back to fiddle