1

I have a scenario of splitting a string field based on newline character chr(10) using pl sql function with a loop and append a tag to the splitted strings(even if there is a empty string in a line).

eg- 1chr(10) 2chr(10) chr(10) 3  should become <a>1</a><a>2</a><a></a><a>3</a>

I have achieved it using an sql query in the following manner

   Select '<test>'
           || replace('1chr(10) 2chr(10) chr(10)3',chr(10),'</test>'||chr(10)||'<test>') 
           || '</test>'
    From your_table;

But I want to get it done using PLSQL function for future uses and if I want to add any other logics. How can I do it using a loop in a plsql function?

1 Answer 1

1

You can use for loop and regexp as follows:

Declare
  Str varchar2(1000) := '1' || chr(10) || '2' || chr(10) || '3';
  Outstr varchar2(2000);
Begin
  For i in 1.. (regexp_count(str, chr(10)) + 1) 
  loop
     Outstr := outstr ||  '<a>' || regexp_substr(str,  '[^'||chr(10)||']',1,i) || '</a>';
  End loop;
  Dbms_output.put_line(outstr);
End;
/

Db<>fiddle

Sign up to request clarification or add additional context in comments.

15 Comments

Hi I get the following error " ORA-06550 PKS-00103 encountered symbol LOOK when expecting the following . (), * %="
ORA-06550 PKS-00103 encountered symbol "LOOP" when expecting the following . (), * %="
Done. Updated the answer and added the db<>fiddle. Please check now!!
thanks but when I added blank line. i.e Str varchar2(1000) := '1' || chr(10) || '2' || chr(10) || chr(10)|| '3'; between 2 and 3 I need tags with empty value but empty tags are shown after 3 and not between 2 and 3. How can I change this to catch that empty line?
wow your a super genious thanks for the great help
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.