-- -- pseudo_contains/2 -- create or replace function pseudo_contains( character varying, character varying ) returns int4 as $$ declare indexed_stuff alias for $1; space_sep_list_untrimmed alias for $2; space_sep_list text; upper_indexed_stuff text; -- if you call this var start you get hosed royally first_space integer; score integer; begin space_sep_list := upper(ltrim(rtrim(space_sep_list_untrimmed))); upper_indexed_stuff := upper(indexed_stuff); score := 0; if space_sep_list is null or indexed_stuff is null then return score; end if; loop first_space := position(' ' in space_sep_list); if first_space = 0 then -- one token or maybe end of list if position(space_sep_list in upper_indexed_stuff) <> 0 then return score+10; end if; return score; else -- first_space <> 0 if position(substring(space_sep_list from 1 for first_space-1) in upper_indexed_stuff) <> 0 then score := score + 10; end if; end if; space_sep_list := substring(space_sep_list from first_space+1); end loop; end;$$ language plpgsql;