Solved

String parsing in PL/SQL: how to produce a list using "double-asterisk" separated fields in a VARCHAR2.

Posted on 2008-10-06
5
978 Views
Last Modified: 2013-12-18
Please assume you have the following string, stored in a VARCHAR2(4000):

ASTRING = '**NE**NECOLL1**NECOLL2**NE37509999**';

Using double-asterisk (==> **) as separator, I would like to write a VIEW script:

CREATE OR REPLACE VIEW STRING_SPLITTER AS SELECT ...........

The VIEW STRING_SPLITTER has to produce the following output, if SELECTed:

SELECT * FROM STRING_SPLITTER;

NE
NECOLL1
NECOLL2
NE37509999
4 row(s) selected

Any clues?!
0
Comment
Question by:CRISTIANO_CORRADI
  • 3
  • 2
5 Comments
 
LVL 15

Accepted Solution

by:
Shaju Kumbalath earned 500 total points
ID: 22648800
0
 

Author Comment

by:CRISTIANO_CORRADI
ID: 22648894
Perfect!
0
 

Author Comment

by:CRISTIANO_CORRADI
ID: 22648902
I've customized, for my application, the following solution found in the suggested hypertextual link:


Using a Pipelined Function

An alternative to a standard PL/SQL function (from 9i onwards) is the use of pipelined functions. 

SQL> CREATE TYPE test_type AS TABLE OF VARCHAR2(100);

  2  /
 

Type created.
 

SQL> CREATE OR REPLACE FUNCTION f_convert2(p_list IN VARCHAR2)

  2    RETURN test_type

  3  PIPELINED

  4  AS

  5    l_string       LONG := p_list || ',';

  6    l_comma_index  PLS_INTEGER;

  7    l_index        PLS_INTEGER := 1;

  8  BEGIN

  9    LOOP

 10      l_comma_index := INSTR(l_string, ',', l_index);

 11      EXIT WHEN l_comma_index = 0;

 12      PIPE ROW ( SUBSTR(l_string, l_index, l_comma_index - l_index) );

 13      l_index := l_comma_index + 1;

 14    END LOOP;

 15    RETURN;

 16  END f_convert2;

 17  /
 

Function created.
 

SQL> SELECT * FROM TABLE(f_convert('AAA,BBB,CCC,D'));
 

COLUMN_VALUE

--------------------------------------------------------------------------------

AAA

BBB

CCC

D
 

4 rows selected.

The advantage of pipelined functions is that the PIPE ROW statement means that the whole collection is not populated all at once, as in Table Functions. In that respect, it is much more memory efficient. 

Open in new window

0
 

Author Closing Comment

by:CRISTIANO_CORRADI
ID: 31503358
Thank you very much for your kind cooperation.
0
 
LVL 15

Expert Comment

by:Shaju Kumbalath
ID: 22648916

select answer
from (
select trim(substr(str, instr(str, '**', 1, level) + 2,
instr(str, '**', 1, level + 1) - instr(str, '**', 1, level) - 2)) answer
, level lv
, lag(level, 1, 0) over (order by level) lg
from (
select '**NE**NECOLL1**NECOLL2**NE37509999**' str
from dual)
connect by instr(str, '**', 1, level) > 0)
where answer is not null
and lv != lg;  
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Last record chosen in Oracle Query 3 53
Oracle TEXT search question 9 46
What is the version of ojdbc6.jar 2 37
Concat multi row values of a field in oracle 6 52
Article by: Swadhin
From the Oracle SQL Reference (http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/queries006.htm) we are told that a join is a query that combines rows from two or more tables, views, or materialized views. This article provides a glimps…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now