[FIXED] splitting characters using function in PostgreSQL

Issue

I tried to write function like this in PostgreSQL but I'm getting error like

ERROR: syntax error at or near "elems"
LINE 22: RETURN elems;

I want get output like

input: we@@@ty;rer@@2hjjj output:

we@@@ty 
rer@@2hjjj

please help me to solve this error

CREATE OR REPLACE FUNCTION public.fn_split(
    inputstr text,
    delimeter text)
    RETURNS text[]
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE SECURITY DEFINER PARALLEL UNSAFE
AS $BODY$
DECLARE
   delimeter text;  
    elems text[];
    var text;
arr_len int;
  BEGIN
SELECT unnest(string_to_array(inputstr,delimeter))
INTO elems

   RETURN elems;
  END
$BODY$;

Solution

CREATE OR REPLACE FUNCTION public.fn_split(
    inputstr text,
    delimeter text)
    RETURNS text[]
    LANGUAGE 'plpgsql'

    COST 100
    VOLATILE SECURITY DEFINER 

AS $BODY$
DECLARE 
 elems text[];
BEGIN 
 SELECT string_to_array(inputstr,delimeter) INTO elems;
 RETURN elems;
END; 
$BODY$;

Now call this function like this

SELECT UNNEST(fn_split('1,2,3',',')) as retval

enter image description here

Above is the screenshot which includes function definition and in the first list the command to call this function

Here is the command that you need to execute in order to call this function in the PostgreSQL query window after the creation of the function.

enter image description here



Answered By - maddy23285
Answer Checked By - David Marino (sql.easybugfix Volunteer)

Related Posts

Post a Comment