-- FUNCTION: aws_commons.fgetstringparts(character varying, character varying, integer)

-- DROP FUNCTION IF EXISTS aws_commons.fgetstringparts(character varying, character varying, integer);

CREATE OR REPLACE FUNCTION aws_commons.fgetstringparts(
	par_value character varying,
	par_sep character varying,
	par_part integer)
    RETURNS character varying
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
AS $BODY$
DECLARE
	var_seplen int = LENGTH(concat(par_sep , '.')) - 1;
	var_s int = 1;
-- 	var_e int = strin(par_sep, par_value, var_s);
	var_i int = 1;
	var_sting_array text[];
	var_sting character varying;
	
BEGIN 
	IF par_value IS NULL OR par_sep IS NULL OR par_part IS NULL 
	THEN 
		RETURN NULL;
	END IF;
	
	IF par_part = 0 
	THEN
		RETURN par_value;
	END IF;
	
	var_sting_array = string_to_array(par_value, par_sep) ;
	
-- 	raise notice 'var_sting_array: %',var_sting_array[par_part];
	
	var_sting = var_sting_array[par_part];
-- 	raise notice 'var_sting: %',var_sting;
	
	return trim(var_sting);
END;
$BODY$;

ALTER FUNCTION aws_commons.fgetstringparts(character varying, character varying, integer)
    OWNER TO postgres;
