I want to pass an array like [group1, group2, group3]
and then filter the Postgres table column groups
.
Create or replace function funname(groups text[])
Select *
From tableName
Where groupscolumn @> groups
Also kindly write the function for the same, getting an error while defining character varying []
.
3
Answers
You can use the
unnest
function to convert an array in ‘table’ output and then filter the value that you need, for example:It’s unclear to me what you want, but maybe you are looking for the ANY operator?
This requires that the data type of the
group_column
and the one of the parameter match. E.g. ifgroup_column
istext
orvarchar
the parameter needs to be declare astext[]
. Ifgroup_column
is e.g. aninteger
, the parameter needs to be declared asinteger[]
You can use join for groups array and SELECT WHERE group IN groups for filter: