3

I have a string like below:

Hello Tom

Where are you

What's your name

I want to split the above string with new line and add to array as below :

[Hello Tom,Where are you,What's your name]

Is this possible in PL/SQL ?

0

2 Answers 2

4

Why do you ask Tom's name again when you have already said hello to him?

Anyways.. Yes. You could use the standard string split operation on string delimited by CHR(10) - newline character in Oracle. Then make use of CAST and COLLECT functions to convert it to an array. Here I have used Oracle's built-in collection sys.OdciVarchar2List. In your PL/SQL block you may BULK COLLECT it into any proper collection type that can hold string elements.

SQL Fiddle

WITH t (s)
AS (
    SELECT 'Hello Tom
Where are you
What''s your name'
    FROM DUAL
    )
SELECT CAST ( COLLECT ( REGEXP_SUBSTR(s, '[^' || CHR(10)|| ']+', 1, LEVEL) )
                       AS sys.OdciVarchar2List )  as collection
FROM t CONNECT BY LEVEL <= REGEXP_COUNT(s, '[^' || CHR(10)|| ']+')

Results:

|                               COLLECTION |
|------------------------------------------|
| Hello Tom,Where are you,What's your name |
Sign up to request clarification or add additional context in comments.

Comments

1

Kaushik is probably right and I'm probably wrong, but that's what I understood (by looking at what the OP enclosed into square brackets, as a desired result):

SQL> WITH t (s)
  2  AS (SELECT 'Hello Tom
  3  Where are you
  4  What''s your name'
  5      FROM DUAL
  6      )
  7  select replace(s, chr(10), ',') result
  8  from t;

RESULT
----------------------------------------
Hello Tom,Where are you,What's your name

SQL>

Comments

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.