Task description
Stored procedure shall have input parameters:
- STRING
- DELIMITER //any symbol
- STRING_NUMBER //number of substring to be returned
Input example: STRING => ‘one,two,three’,
DELIMITER => ‘e’,
STRING_NUMBER => null
Output shall be: ‘on’
‘,two,thr’
‘’
‘’
If STRING_NUMBER => 2, output shall be: ‘,two,thr’
It is a job interview question.
I have done the following:
CREATE OR REPLACE PROCEDURE substring
(STRNG IN VARCHAR2,DELIMITER IN VARCHAR2, STRING_NUMBER IN NUMBER)
IS
replace_string VARCHAR2(100);
instring NUMBER;
comma_counter NUMBER;
substring VARCHAR2(100);
BEGIN
replace_string:= REPLACE(STRNG,DELIMITER);
comma_counter:=REGEXP_COUNT(STRNG,',');
instring:=INSTR(STRNG,',',1,comma_counter-STRING_NUMBER+1);
substring:=SUBSTR(replace_string,instring);
DBMS_OUTPUT.PUT_LINE(replace_string);
DBMS_OUTPUT.PUT_LINE(','||substring);
END;
set serveroutput on
BEGIN
substring('one,two,three,four','e',2);
END;
Now, what is my concern:
1.