ranyeli
6/26/2018 - 7:22 PM

Using Integer Arrays

Casts an array of varchar to an array of integers and select the array as if it were a record/table/row

CREATE or replace FUNCTION my_method(
    varchar)
  RETURNS varchar
  AS
$body$ 
DECLARE

       reg varchar;
BEGIN
    
    select into reg cast(string_agg(f_descripcion, ', ') as varchar) from public.t_almacen where f_iddepto in (
    select unnest(string_to_array($1,',')::int[])
    );
    if found then

          return reg;

    else

        return '';

    end if;
    
END;
$body$
  LANGUAGE 'plpgsql'
  VOLATILE
  CALLED ON NULL INPUT
  SECURITY INVOKER
  COST 100;

select my_method(CAST('1,3,5,6' as varchar));