Search code examples
sqlplsqlprocedure

PL/SQL Create procedure which returns two string values


I am new to SQL and am trying to create a procedure which takes in a 6 character string eg. 'POLICE' and then will return two substring values eg. 'POL', 'ICE'

This is what I have tried so far:

procedure split_string(inputString in string, substr1 in OUT string, substr2 in OUT string)
is
begin
      substr1 := SUBSTR(inputString, 1, 3);
      substr2 := SUBSTR(inputString, 4, 3);
end split_string;

However this is not working, i am also unsure on how to call this function and use its values elsewhere in the code.

Is it something like this:

split_string('POLICE') ?

Solution

  • Use this:

    CREATE OR replace procedure split_string(inputString in varchar2, 
                                             substr1  IN OUT varchar2, 
                                             substr2  IN OUT varchar2
                                             )
    As
    
    begin
          substr1 := SUBSTR(inputString, 1, 3);
          substr2 := SUBSTR(inputString, 4, 3);
    END split_string;
    

    Usage:

    DECLARE
       a   VARCHAR2 (10);
       b   VARCHAR2 (20);
    BEGIN
       split_string ('POLICE', a, b);
    
       DBMS_OUTPUT.put_line (a || ',' || b);
    END;
    

    Output

    SQL>     
    
        POL,ICE
    
        PL/SQL procedure successfully completed.