Skip to content

Instantly share code, notes, and snippets.

@pirosuke
Created August 23, 2015 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pirosuke/0f82ca8ecb4f21c3b454 to your computer and use it in GitHub Desktop.
Save pirosuke/0f82ca8ecb4f21c3b454 to your computer and use it in GitHub Desktop.
in_list function for PostgreSQL
CREATE OR REPLACE FUNCTION SF_IN_LIST(
p_list1 text[],
p_list2 text[]
) RETURNS BOOLEAN AS $$
DECLARE
w_is_in_list BOOLEAN := true;
BEGIN
IF p_list1 IS NULL OR p_list2 IS NULL THEN
RETURN false;
END IF;
IF array_length(p_list1, 1) = 0 OR array_length(p_list2, 1) = 0 THEN
RETURN false;
END IF;
FOR i IN 1..array_length(p_list1, 1) LOOP
IF p_list1[i] = ANY(p_list2) THEN
ELSE
w_is_in_list := false;
EXIT;
END IF;
END LOOP;
RETURN w_is_in_list;
END;
$$ LANGUAGE plpgsql;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment