CREATE OR REPLACE FUNCTION fn_split
(
	IN _str character varying,
	IN _sepr character varying
)
RETURNS TABLE(number integer, split character varying) 
AS
$BODY$
declare	_idx integer;
	_prev integer = 0;
begin
	_idx := position(_sepr in _str);
	drop table if exists _splits;
	create temp table _splits(number serial, split varchar);
	while position(_sepr in substring(_str from _prev)) <>0 
	LOOP
		insert into _splits(split)
		values(substring(_str from _prev for _idx-_prev));
		_prev:=_idx +1;
		_idx=_prev-1+ position(_sepr in substring(_str from _prev));
	end LOOP;
	insert into _splits(split) 
	values(substring(_str from _idx+1));
	return query (select * from _splits);
drop table _splits;
end;
$BODY$
  LANGUAGE plpgsql volatile
  COST 100
  ROWS 1000;
ALTER FUNCTION fn_split(character varying, character varying)
  OWNER TO postgres;