Split function in oracle to comma separated values with automatic sequence - oracle

Need Split function which will take two parameters, string to split and delimiter to split the string and return a table with columns Id and Data.And how to call Split function which will return a table with columns Id and Data. Id column will contain sequence and data column will contain data of the string.
Eg.
SELECT*FROM Split('A,B,C,D',',')
Result Should be in below format:
|Id | Data
-- ----
|1 | A |
|2 | B |
|3 | C |
|4 | D |

Here is how you could create such a table:
SELECT LEVEL AS id, REGEXP_SUBSTR('A,B,C,D', '[^,]+', 1, LEVEL) AS data
FROM dual
CONNECT BY REGEXP_SUBSTR('A,B,C,D', '[^,]+', 1, LEVEL) IS NOT NULL;
With a little bit of tweaking (i.e., replacing the , in [^,] with a variable) you could write such a function to return a table.

There are multiple options. See Split single comma delimited string into rows in Oracle
You just need to add LEVEL in the select list as a column, to get the sequence number to each row returned. Or, ROWNUM would also suffice.
Using any of the below SQLs, you could include them into a FUNCTION.
INSTR in CONNECT BY clause:
SQL> WITH DATA AS
2 ( SELECT 'word1, word2, word3, word4, word5, word6' str FROM dual
3 )
4 SELECT trim(regexp_substr(str, '[^,]+', 1, LEVEL)) str
5 FROM DATA
6 CONNECT BY instr(str, ',', 1, LEVEL - 1) > 0
7 /
STR
----------------------------------------
word1
word2
word3
word4
word5
word6
6 rows selected.
SQL>
REGEXP_SUBSTR in CONNECT BY clause:
SQL> WITH DATA AS
2 ( SELECT 'word1, word2, word3, word4, word5, word6' str FROM dual
3 )
4 SELECT trim(regexp_substr(str, '[^,]+', 1, LEVEL)) str
5 FROM DATA
6 CONNECT BY regexp_substr(str , '[^,]+', 1, LEVEL) IS NOT NULL
7 /
STR
----------------------------------------
word1
word2
word3
word4
word5
word6
6 rows selected.
SQL>
REGEXP_COUNT in CONNECT BY clause:
SQL> WITH DATA AS
2 ( SELECT 'word1, word2, word3, word4, word5, word6' str FROM dual
3 )
4 SELECT trim(regexp_substr(str, '[^,]+', 1, LEVEL)) str
5 FROM DATA
6 CONNECT BY LEVEL
Using XMLTABLE
SQL> WITH DATA AS
2 ( SELECT 'word1, word2, word3, word4, word5, word6' str FROM dual
3 )
4 SELECT trim(COLUMN_VALUE) str
5 FROM DATA, xmltable(('"' || REPLACE(str, ',', '","') || '"'))
6 /
STR
------------------------------------------------------------------------
word1
word2
word3
word4
word5
word6
6 rows selected.
SQL>
Using MODEL clause:
SQL> WITH t AS
2 (
3 SELECT 'word1, word2, word3, word4, word5, word6' str
4 FROM dual ) ,
5 model_param AS
6 (
7 SELECT str AS orig_str ,
8 ','
9 || str
10 || ',' AS mod_str ,
11 1 AS start_pos ,
12 Length(str) AS end_pos ,
13 (Length(str) - Length(Replace(str, ','))) + 1 AS element_count ,
14 0 AS element_no ,
15 ROWNUM AS rn
16 FROM t )
17 SELECT trim(Substr(mod_str, start_pos, end_pos-start_pos)) str
18 FROM (
19 SELECT *
20 FROM model_param MODEL PARTITION BY (rn, orig_str, mod_str)
21 DIMENSION BY (element_no)
22 MEASURES (start_pos, end_pos, element_count)
23 RULES ITERATE (2000)
24 UNTIL (ITERATION_NUMBER+1 = element_count[0])
25 ( start_pos[ITERATION_NUMBER+1] = instr(cv(mod_str), ',', 1, cv(element_no)) + 1,
26 end_pos[iteration_number+1] = instr(cv(mod_str), ',', 1, cv(element_no) + 1) ) )
27 WHERE element_no != 0
28 ORDER BY mod_str ,
29 element_no
30 /
STR
------------------------------------------
word1
word2
word3
word4
word5
word6
6 rows selected.
SQL>
You could also use DBMS_UTILITY package provided by Oracle. It provides various utility subprograms. One such useful utility is COMMA_TO_TABLE procedure, which converts a comma-delimited list of names into a PL/SQL table of names.
Read DBMS_UTILITY.COMMA_TO_TABLE

Oracle Setup:
CREATE OR REPLACE FUNCTION split_String(
i_str IN VARCHAR2,
i_delim IN VARCHAR2 DEFAULT ','
) RETURN SYS.ODCIVARCHAR2LIST DETERMINISTIC
AS
p_result SYS.ODCIVARCHAR2LIST := SYS.ODCIVARCHAR2LIST();
p_start NUMBER(5) := 1;
p_end NUMBER(5);
c_len CONSTANT NUMBER(5) := LENGTH( i_str );
c_ld CONSTANT NUMBER(5) := LENGTH( i_delim );
BEGIN
IF c_len > 0 THEN
p_end := INSTR( i_str, i_delim, p_start );
WHILE p_end > 0 LOOP
p_result.EXTEND;
p_result( p_result.COUNT ) := SUBSTR( i_str, p_start, p_end - p_start );
p_start := p_end + c_ld;
p_end := INSTR( i_str, i_delim, p_start );
END LOOP;
IF p_start <= c_len + 1 THEN
p_result.EXTEND;
p_result( p_result.COUNT ) := SUBSTR( i_str, p_start, c_len - p_start + 1 );
END IF;
END IF;
RETURN p_result;
END;
/
Query
SELECT ROWNUM AS ID,
COLUMN_VALUE AS Data
FROM TABLE( split_String( 'A,B,C,D' ) );
Output:
ID DATA
-- ----
1 A
2 B
3 C
4 D

If you need a function try this.
First we'll create a type:
CREATE OR REPLACE TYPE T_TABLE IS OBJECT
(
Field1 int
, Field2 VARCHAR(25)
);
CREATE TYPE T_TABLE_COLL IS TABLE OF T_TABLE;
/
Then we'll create the function:
CREATE OR REPLACE FUNCTION TEST_RETURN_TABLE
RETURN T_TABLE_COLL
IS
l_res_coll T_TABLE_COLL;
l_index number;
BEGIN
l_res_coll := T_TABLE_COLL();
FOR i IN (
WITH TAB AS
(SELECT '1001' ID, 'A,B,C,D,E,F' STR FROM DUAL
UNION
SELECT '1002' ID, 'D,E,F' STR FROM DUAL
UNION
SELECT '1003' ID, 'C,E,G' STR FROM DUAL
)
SELECT id,
SUBSTR(STR, instr(STR, ',', 1, lvl) + 1, instr(STR, ',', 1, lvl + 1) - instr(STR, ',', 1, lvl) - 1) name
FROM
( SELECT ',' || STR || ',' AS STR, id FROM TAB
),
( SELECT level AS lvl FROM dual CONNECT BY level <= 100
)
WHERE lvl <= LENGTH(STR) - LENGTH(REPLACE(STR, ',')) - 1
ORDER BY ID, NAME)
LOOP
IF i.ID = 1001 THEN
l_res_coll.extend;
l_index := l_res_coll.count;
l_res_coll(l_index):= T_TABLE(i.ID, i.name);
END IF;
END LOOP;
RETURN l_res_coll;
END;
/
Now we can select from it:
select * from table(TEST_RETURN_TABLE());
Output:
SQL> select * from table(TEST_RETURN_TABLE());
FIELD1 FIELD2
---------- -------------------------
1001 A
1001 B
1001 C
1001 D
1001 E
1001 F
6 rows selected.
Obviously you'd need to replace the WITH TAB AS... bit with where you would be getting your actual data from.
Credit Credit

Use this 'Split' function:
CREATE OR REPLACE FUNCTION Split (p_str varchar2) return sys_refcursor is
v_res sys_refcursor;
begin
open v_res for
WITH TAB AS
(SELECT p_str STR FROM DUAL)
select substr(STR, instr(STR, ',', 1, lvl) + 1, instr(STR, ',', 1, lvl + 1) - instr(STR, ',', 1, lvl) - 1) name
from
( select ',' || STR || ',' as STR from TAB ),
( select level as lvl from dual connect by level <= 100 )
where lvl <= length(STR) - length(replace(STR, ',')) - 1;
return v_res;
end;
You can't use this function in select statement like you described in question, but I hope you will find it still useful.
EDIT: Here are steps you need to do.
1. Create Object: create or replace type empy_type as object(value varchar2(512))
2. Create Type: create or replace type t_empty_type as table of empy_type
3. Create Function:
CREATE OR REPLACE FUNCTION Split (p_str varchar2) return sms.t_empty_type is
v_emptype t_empty_type := t_empty_type();
v_cnt number := 0;
v_res sys_refcursor;
v_value nvarchar2(128);
begin
open v_res for
WITH TAB AS
(SELECT p_str STR FROM DUAL)
select substr(STR, instr(STR, ',', 1, lvl) + 1, instr(STR, ',', 1, lvl + 1) - instr(STR, ',', 1, lvl) - 1) name
from
( select ',' || STR || ',' as STR from TAB ),
( select level as lvl from dual connect by level <= 100 )
where lvl <= length(STR) - length(replace(STR, ',')) - 1;
loop
fetch v_res into v_value;
exit when v_res%NOTFOUND;
v_emptype.extend;
v_cnt := v_cnt + 1;
v_emptype(v_cnt) := empty_type(v_value);
end loop;
close v_res;
return v_emptype;
end;
Then just call like this:
SELECT * FROM (TABLE(split('a,b,c,d,g')))

This function returns the nth part of input string MYSTRING.
Second input parameter is separator ie., SEPARATOR_OF_SUBSTR
and the third parameter is Nth Part which is required.
Note: MYSTRING should end with the separator.
create or replace FUNCTION PK_GET_NTH_PART(MYSTRING VARCHAR2,SEPARATOR_OF_SUBSTR VARCHAR2,NTH_PART NUMBER)
RETURN VARCHAR2
IS
NTH_SUBSTR VARCHAR2(500);
POS1 NUMBER(4);
POS2 NUMBER(4);
BEGIN
IF NTH_PART=1 THEN
SELECT REGEXP_INSTR(MYSTRING,SEPARATOR_OF_SUBSTR, 1, 1) INTO POS1 FROM DUAL;
SELECT SUBSTR(MYSTRING,0,POS1-1) INTO NTH_SUBSTR FROM DUAL;
ELSE
SELECT REGEXP_INSTR(MYSTRING,SEPARATOR_OF_SUBSTR, 1, NTH_PART-1) INTO POS1 FROM DUAL;
SELECT REGEXP_INSTR(MYSTRING,SEPARATOR_OF_SUBSTR, 1, NTH_PART) INTO POS2 FROM DUAL;
SELECT SUBSTR(MYSTRING,POS1+1,(POS2-POS1-1)) INTO NTH_SUBSTR FROM DUAL;
END IF;
RETURN NTH_SUBSTR;
END;
Hope this helps some body, you can use this function like this in a loop to get all the values separated:
SELECT REGEXP_COUNT(MYSTRING, '~', 1, 'i') INTO NO_OF_RECORDS FROM DUAL;
WHILE NO_OF_RECORDS>0
LOOP
PK_RECORD :=PK_GET_NTH_PART(MYSTRING,'~',NO_OF_RECORDS);
-- do some thing
NO_OF_RECORDS :=NO_OF_RECORDS-1;
END LOOP;
Here NO_OF_RECORDS,PK_RECORD are temp variables.
Hope this helps.

Best Query For comma separated
in This Query we Convert Rows To Column ...
SELECT listagg(BL_PRODUCT_DESC, ', ') within
group( order by BL_PRODUCT_DESC) PROD
FROM GET_PRODUCT
-- WHERE BL_PRODUCT_DESC LIKE ('%WASH%')
WHERE Get_Product_Type_Id = 6000000000007

Created PL/SQL function that can split string by specified delimiter and return result as VARRAY.
CREATE OR REPLACE FUNCTION split(p_parameters VARCHAR2, p_delimiter VARCHAR2) RETURN string_varray AS
v_delimiter_position NUMBER := 0;
v_read_position NUMBER :=1;
v_list string_varray := string_varray();
v_substring VARCHAR2(4000);
FUNCTION normalize(v_substring VARCHAR2, p_delimiter VARCHAR2) RETURN VARCHAR2 AS
BEGIN
RETURN trim(TRAILING p_delimiter FROM trim(BOTH ' ' FROM v_substring));
END normalize;
BEGIN
LOOP
v_delimiter_position := instr(p_parameters, p_delimiter, v_read_position);
IF v_delimiter_position = 0 THEN
v_delimiter_position := LENGTH(p_parameters);
END IF;
v_substring := substr(p_parameters, v_read_position, v_delimiter_position-v_read_position+1);
v_list.EXTEND;
v_list(v_list.LAST) := normalize(v_substring, p_delimiter);
v_read_position := v_delimiter_position+1;
IF v_delimiter_position = LENGTH(p_parameters) THEN
EXIT;
END IF;
END LOOP;
RETURN v_list;
END split;
string_varray is VARRAY of VARCHAR2(4000) type. Function also removes whitespaces and the start and end of each value. Invocation example:
select * from table(split('zaa, dddd,ccc', ','));
Will produce three rows in output: zaa dddd ccc

begin
for rec in (select * from table(split('shfgjsdfg,242535', ',')))
loop
dbms_output.put_line(rec.COLUMN_VALUE);
end loop;
end;
-- Output
shfgjsdfg
242535

Try like below
select
split.field(column_name,1,',','"') name1,
split.field(column_name,2,',','"') name2
from table_name

Related

How to find last iteration in below for loop

I have following Oracle query. I am trying to find last index in the iteration, In other words, I want to print the result only in last step. But I have no success
set serveroutput on
DECLARE
str VARCHAR2(100) := 'a,c,v,b';
V_CMP_MUMBER VARCHAR2(20);
V_CMP_MUMBERS VARCHAR2(200);
BEGIN
FOR i IN
(SELECT trim(regexp_substr(str, '[^,]+', 1, LEVEL)) l
FROM dual
CONNECT BY LEVEL <= regexp_count(str, ',')+1
)
LOOP
select cn INTO V_CMP_MUMBER from VP where NAME=i.l;
V_CMP_MUMBERS := V_CMP_MUMBERS || ',' || v_cmp_mumber;
dbms_output.put_line(REGEXP_REPLACE(V_CMP_MUMBERS,'^,', '' ));
END LOOP;
END;
/
Don't use a loop and do it all in a single SQL query:
DECLARE
str VARCHAR2(100) := 'a,c,v,b';
V_CMP_MUMBERS VARCHAR2(200);
v_count PLS_INTEGER;
BEGIN
SELECT COUNT(*),
LISTAGG(cn, ',')
WITHIN GROUP (ORDER BY INSTR(','||str||',', ','||name||','))
INTO v_count,
V_CMP_MUMBERS
FROM VP
WHERE INSTR(','||str||',', ','||name||',') > 0;
dbms_output.put_line('Number of rows matched: ' || v_count);
dbms_output.put_line('Matches: ' || V_CMP_MUMBERS);
END;
/
Which, for the sample data:
CREATE TABLE vp (name, cn) AS
SELECT 'a', 'aaa' FROM DUAL UNION ALL
SELECT 'b', 'bbb' FROM DUAL UNION ALL
SELECT 'c', 'ccc' FROM DUAL UNION ALL
SELECT 'v', 'vvv' FROM DUAL;
Outputs:
Number of rows matched: 4
Matches: aaa,ccc,vvv,bbb
db<>fiddle here
Move dbms_output.put_line call out of the loop.
For my sample table:
SQL> select * from vp;
CN N
---------- -
100 a
200 b
300 c
400 v
SQL>
result is then
SQL> DECLARE
2 str VARCHAR2(100) := 'a,c,v,b';
3 V_CMP_MUMBER VARCHAR2(20);
4 V_CMP_MUMBERS VARCHAR2(200);
5 l_last_index number := 0;
6 BEGIN
7 FOR i IN
8 (SELECT trim(regexp_substr(str, '[^,]+', 1, LEVEL)) l
9 FROM dual
10 CONNECT BY LEVEL <= regexp_count(str, ',')+1
11 )
12 LOOP
13 l_last_index := l_last_index + 1;
14 select cn INTO V_CMP_MUMBER from VP where NAME=i.l;
15 V_CMP_MUMBERS := V_CMP_MUMBERS || ',' || v_cmp_mumber;
16 END LOOP;
17 dbms_output.put_line('Last index = ' || l_last_index);
18 dbms_output.put_line(REGEXP_REPLACE(V_CMP_MUMBERS,'^,', '' ));
19 END;
20 /
Last index = 4
100,300,400,200
PL/SQL procedure successfully completed.
SQL>

How to retain the same case in the text in Oracle?

I have a table with columns word and sentence. The idea is to replace the words in the sentence with the link(includes the word itself) if the word is found in the word column. The query below replaces perfectly but since the link is constructed from the temp.word column, the case of the words in the sentence is changed to the case of the words in the word column. Is there a way to retain the same case in the sentence itself?
Create table temp(
id NUMBER,
word VARCHAR2(1000),
sentence VARCHAR2(2000)
);
insert into temp
SELECT 1,'automation testing', 'automtestingation Testing is popular kind of testing' FROM DUAL UNION ALL
SELECT 2,'testing','manual testing' FROM DUAL UNION ALL
SELECT 3,'manual testing','this is an old method of testing' FROM DUAL UNION ALL
SELECT 4,'punctuation','automation testing,manual testing,punctuation,automanual testing-testing' FROM DUAL UNION ALL
SELECT 5,'B-number analysis','B-number analysis table' FROM DUAL UNION ALL
SELECT 6,'B-number analysis table','testing B-number analysis' FROM DUAL UNION ALL
SELECT 7,'Not Matched','testing testing testing' FROM DUAL;
with words(id, word, word_length, search1, replace1, search2, replace2) as (
select id, word, length(word),
'(^|\W)' || REGEXP_REPLACE(word, '([][)(}{|^$\.*+?])', '\\\1') || '($|\W)',
'\1{'|| id ||'}\2',
'{'|| id ||'}',
'http://localhost/' || id || '/<u>' || word || '</u>'
FROM temp
)
, joined_data as (
select w.search1, w.replace1, w.search2, w.replace2,
s.rowid s_rid, s.sentence,
row_number() over(partition by s.rowid order by word_length desc) rn
from words w
join temp s
on instr(UPPER(s.sentence), UPPER(w.word)) > 0
and regexp_like(s.sentence, w.search1)
)
, unpivoted_data as (
select S_RID, SENTENCE, PHASE, SEARCH_STRING, REPLACE_STRING,
row_number() over(partition by s_rid order by phase, rn) rn,
case when row_number() over(partition by s_rid order by phase, rn)
= count(*) over(partition by s_rid)
then 1
else 0
end is_last
from joined_data
unpivot(
(search_string, replace_string)
for phase in ( (search1, replace1) as 1, (search2, replace2) as 2 ))
)
, replaced_data(S_RID, RN, is_last, SENTENCE) as (
select S_RID, RN, is_last,
regexp_replace(SENTENCE, search_string, replace_string,1,0,'i')
from unpivoted_data
where rn = 1
union all
select n.S_RID, n.RN, n.is_last,
case when n.phase = 1
then regexp_replace(o.SENTENCE, n.search_string, n.replace_string,1,0,'i')
else replace(o.SENTENCE, n.search_string, n.replace_string)
end
from unpivoted_data n
join replaced_data o
on o.s_rid = n.s_rid and n.rn = o.rn + 1
)
select s_rid, sentence from replaced_data
where is_last = 1
order by s_rid;
For example, for id = 1, the sentence is automtestingation Testing is popular kind of testing
After replacement it will be automtestingation http://localhost/2/<u>testing</u> is popular kind of http://localhost/2/<u>testing</u>.
The word Testing is replaced with testing(from the temp.word column).
The expected outcome is
automtestingation http://localhost/2/<u>Testing</u> is popular kind of http://localhost/2/<u>testing</u>
Oracle Setup:
Create table temp(
id NUMBER,
word VARCHAR2(1000),
Sentence VARCHAR2(2000)
);
insert into temp
SELECT 1,'automation testing', 'automtestingation TeStInG TEST is popular kind of testing' FROM DUAL UNION ALL
SELECT 2,'testing','manual testing' FROM DUAL UNION ALL
select 2,'test', 'test' FROM DUAL UNION ALL
SELECT 3,'manual testing','this is an old method of testing' FROM DUAL UNION ALL
SELECT 4,'punctuation','automation testing,manual testing,punctuation,automanual testing-testing' FROM DUAL UNION ALL
SELECT 5,'B-number analysis','B-number analysis table' FROM DUAL UNION ALL
SELECT 6,'B-number analysis table','testing B-number analysis' FROM DUAL UNION ALL
SELECT 7,'Not Matched','testing testing testing' FROM DUAL UNION ALL
SELECT 8,'^[($','testing characters ^[($ that need escaping in a regular expression' FROM DUAL;
SQL Types:
CREATE TYPE stringlist IS TABLE OF VARCHAR2(4000);
/
CREATE TYPE intlist IS TABLE OF NUMBER(20,0);
/
PL/SQL Function:
CREATE FUNCTION replace_words(
word_list IN stringlist,
id_list IN intlist,
sentence IN temp.sentence%TYPE
) RETURN temp.sentence%TYPE
IS
p_sentence temp.sentence%TYPE := UPPER( sentence );
p_pos PLS_INTEGER := 1;
p_min_word_index PLS_INTEGER;
p_word_index PLS_INTEGER;
p_start PLS_INTEGER;
p_index PLS_INTEGER;
o_sentence temp.sentence%TYPE;
BEGIN
LOOP
p_min_word_index := NULL;
p_index := NULL;
FOR i IN 1 .. word_list.COUNT LOOP
p_word_index := p_pos;
LOOP
p_word_index := INSTR( p_sentence, word_list(i), p_word_index );
EXIT WHEN p_word_index = 0;
IF ( p_word_index > 1
AND REGEXP_LIKE( SUBSTR( p_sentence, p_word_index - 1, 1 ), '\w' )
)
OR REGEXP_LIKE( SUBSTR( p_sentence, p_word_index + LENGTH( word_list(i) ), 1 ), '\w' )
THEN
p_word_index := p_word_index + 1;
CONTINUE;
END IF;
IF p_min_word_index IS NULL OR p_word_index < p_min_word_index THEN
p_min_word_index := p_word_index;
p_index := i;
END IF;
EXIT;
END LOOP;
END LOOP;
IF p_index IS NULL THEN
o_sentence := o_sentence || SUBSTR( sentence, p_pos );
EXIT;
ELSE
o_sentence := o_sentence
|| SUBSTR( sentence, p_pos, p_min_word_index - p_pos )
|| 'http://localhost/'
|| id_list(p_index)
|| '/<u>'
|| SUBSTR( sentence, p_min_word_index, LENGTH( word_list( p_index ) ) )
|| '</u>';
p_pos := p_min_word_index + LENGTH( word_list( p_index ) );
END IF;
END LOOP;
RETURN o_sentence;
END;
/
Merge:
MERGE INTO temp dst
USING (
WITH lists ( word_list, id_list ) AS (
SELECT CAST(
COLLECT(
UPPER( word )
ORDER BY LENGTH( word ) DESC, UPPER( word ) ASC, ROWNUM
)
AS stringlist
),
CAST(
COLLECT(
id
ORDER BY LENGTH( word ) DESC, UPPER( word ) ASC, ROWNUM
)
AS intlist
)
FROM temp
)
SELECT t.ROWID rid,
replace_words(
word_list,
id_list,
sentence
) AS replaced_sentence
FROM temp t
CROSS JOIN lists
) src
ON ( dst.ROWID = src.RID )
WHEN MATCHED THEN
UPDATE SET sentence = src.replaced_sentence;
Output:
SELECT * FROM temp;
ID | WORD | SENTENCE
-: | :---------------------- | :---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 | automation testing | automtestingation http://localhost/2/<u>TeStInG</u> http://localhost/2/<u>TEST</u> is popular kind of http://localhost/2/<u>testing</u>
2 | testing | http://localhost/3/<u>manual testing</u>
2 | test | http://localhost/2/<u>test</u>
3 | manual testing | this is an old method of http://localhost/2/<u>testing</u>
4 | punctuation | http://localhost/1/<u>automation testing</u>,http://localhost/3/<u>manual testing</u>,http://localhost/4/<u>punctuation</u>,automanual http://localhost/2/<u>testing</u>-http://localhost/2/<u>testing</u>
5 | B-number analysis | http://localhost/6/<u>B-number analysis table</u>
6 | B-number analysis table | http://localhost/2/<u>testing</u> http://localhost/5/<u>B-number analysis</u>
7 | Not Matched | http://localhost/2/<u>testing</u> http://localhost/2/<u>testing</u> http://localhost/2/<u>testing</u>
8 | ^[($ | http://localhost/2/<u>testing</u> characters http://localhost/8/<u>^[($</u> that need escaping in a regular expression
db<>fiddle here
While there's certainly a way to do this in a single SQL statement, I think this problem is better solved with a separate function:
create or replace function replace_words(p_word varchar2, p_sentence varchar2) return varchar2 is
v_match_position number;
v_match_count number := 0;
v_new_sentence varchar2(4000) := p_sentence;
begin
--Find all matches.
loop
--Find Nth case-insensitive match
v_match_count := v_match_count + 1;
v_match_position := regexp_instr(
srcstr => v_new_sentence,
pattern => p_word,
occurrence => v_match_count,
modifier => 'i');
exit when v_match_position = 0;
--Insert text, instead of replacing, to keep the original case.
v_new_sentence :=
substr(v_new_sentence, 1, v_match_position - 1) || 'http://localhost/2/<u>'||
substr(v_new_sentence, v_match_position, length(p_word)) || '</u>'||
substr(v_new_sentence, v_match_position + length(p_word));
end loop;
return v_new_sentence;
end;
/
Then the SQL query looks like this:
select id, word, sentence, replace_words(word, sentence) from temp;

pl/sql function to parse string to token in oracle

I have written this code
Select replace (upper(substr(div_no,1,3), ',' ','',''')
from dual
I want when user write div_no like this ('v0e200,q0e600') must return
v0e,q0e
If I have a string like 's05200 , s02700' I want to take first three characters, like 's05 ',' s02'
It seems you're looking for the translate() function :
SQL> select translate ('vwe200,qwe600', ',1234567890', ',')
2 from dual;
TRANSLA
-------
vwe,qwe
SQL>
So your revised test data clarifies the requirement. Here is a regex solution for extracting the first three characters from each segment of a string:
SQL> with testdata as (
2 select 'vwe200,qwe600' as str from dual union all
3 select 's05200 , e0300' as str from dual
4 )
5 select regexp_replace(str, '([a-z0-9]{3})([a-z0-9]*)(,?)','\1\3')
6 from testdata;
REGEXP_REPLACE(STR,'([A-Z0-9]{3})([A-Z0-9]*)(,?)','\1\3')
--------------------------------------------------------------------------------
vwe,qwe
s05 , e03
SQL>
If removing the trailing spaces is required it can be done by adjusting the regex pattern :
SQL> with testdata as (
2 select 'vwe200,qwe600' as str from dual union all
3 select 's05200 , e0300' as str from dual
4 )
5 select regexp_replace(str, '( *)([a-z0-9]{3})([a-z0-9 ]*)(,?)','\2\4')
6 from testdata;
REGEXP_REPLACE(STR,'([A-Z0-9]{3})([A-Z0-9]*)(,?)','\1\3')
--------------------------------------------------------------------------------
vwe,qwe
s05,e03
SQL>
Injecting additional quotes is a use for the replace() function:
SQL> select replace('s05 , e03', ',', ''',''') from dual;
REPLACE('S0
-----------
s05 ',' e03
SQL>
Use the output from the previous answer as the input for this one.
Oracle 12c Query:
with FUNCTION getFirst3CharsOfDelimitedList(
str IN VARCHAR2
) RETURN VARCHAR2
IS
val VARCHAR2(4000);
i INTEGER := 1;
occ INTEGER := 1;
BEGIN
val := SUBSTR( str, i, 3 );
LOOP
i := INSTR( str, ',', 1, occ );
EXIT WHEN i = 0;
LOOP
EXIT WHEN SUBSTR( str, i + 1, 1 ) <> ' ';
i := i + 1;
END LOOP;
val := val || ',' || SUBSTR( str, i + 1, 3 );
occ := occ + 1;
END LOOP;
RETURN val;
END getFirst3CharsOfDelimitedList;
testdata as (
select 'vwe200,qwe600' as str from dual union all
select 's05200 , e0300' as str from dual
)
SELECT getFirst3CharsOfDelimitedList( str ) FROM testdata;
Output:
GETFIRST3CHARSOFDELIMITEDLIST(STR)
----------------------------------
vwe,qwe
s05,e03

How to sort multiline text in Oracle db?

I need to create a script that will sort (and update) all values in column A for all records in table B if the text in the field is in more than one line.
Any ideas how to do it? I'm a begginer in Oracle world so any help is appreciated.
Table B
id column A
1 aaa
2 bb \n aa\n dd \n cc
3 bb \n aa
*(\n is new line)
I need it to be
Table B
id column A
1 aaa
2 aa \n bb\n cc \n dd
3 aa \n bb
Oracle Setup:
CREATE TABLE Table_B ( id, column_A ) AS
SELECT 1, 'aaa' FROM DUAL UNION ALL
SELECT 2, 'bb ' || CHR(10) || ' aa' || CHR(10) || ' dd ' || CHR(10) || ' cc' FROM DUAL UNION ALL
SELECT 3, 'bb ' || CHR(10) || ' aa' FROM DUAL;
Also uses my SPLIT_STRING function:
CREATE OR REPLACE FUNCTION split_String(
i_str IN VARCHAR2,
i_delim IN VARCHAR2 DEFAULT ','
) RETURN SYS.ODCIVARCHAR2LIST DETERMINISTIC
AS
p_result SYS.ODCIVARCHAR2LIST := SYS.ODCIVARCHAR2LIST();
p_start NUMBER(5) := 1;
p_end NUMBER(5);
c_len CONSTANT NUMBER(5) := LENGTH( i_str );
c_ld CONSTANT NUMBER(5) := LENGTH( i_delim );
BEGIN
IF c_len > 0 THEN
p_end := INSTR( i_str, i_delim, p_start );
WHILE p_end > 0 LOOP
p_result.EXTEND;
p_result( p_result.COUNT ) := SUBSTR( i_str, p_start, p_end - p_start );
p_start := p_end + c_ld;
p_end := INSTR( i_str, i_delim, p_start );
END LOOP;
IF p_start <= c_len + 1 THEN
p_result.EXTEND;
p_result( p_result.COUNT ) := SUBSTR( i_str, p_start, c_len - p_start + 1 );
END IF;
END IF;
RETURN p_result;
END;
/
Query:
SELECT id,
LISTAGG( COLUMN_VALUE, CHR(10) )
WITHIN GROUP ( ORDER BY TRIM(COLUMN_VALUE) )
AS sorted_column_a
FROM Table_B B,
TABLE( split_String( b.column_a, CHR(10) ) ) s
GROUP BY ID
Output
ID SORTED_COLUMN_A
---------- ----------------
1 aaa
2 aa
bb
cc
dd
3 aa
bb
Update
UPDATE TABLE_B b
SET column_a = ( SELECT LISTAGG( COLUMN_VALUE, CHR(10) )
WITHIN GROUP ( ORDER BY TRIM(COLUMN_VALUE) )
FROM TABLE( split_String( b.column_a, CHR(10) ) ) );
You can try this, which delimits individual value of text using '\n' and sort it. And later merge it in ascending order that you mentioned.
with mydata(id,text)
as
(
select 1, replace('aaa','\n',chr(10)) from dual
union all
select 2, replace('bb \n aa\n dd \n cc','\n',chr(10)) from dual
union all
select 3, replace('bb \n aa','\n',chr(10)) from dual
)
SELECT ID,REPLACE(LISTAGG(text_splitted,chr(10)) WITHIN GROUP (ORDER BY trim(text_splitted)),CHR(10),'\n')
FROM
(
SELECT t1.id,
REGEXP_SUBSTR(t1.text, '([^'||chr(10)||'])+', 1, t2.COLUMN_VALUE) text_splitted
FROM mydata t1 CROSS JOIN
TABLE
(
CAST
(
MULTISET
(
SELECT LEVEL
FROM DUAL
CONNECT BY LEVEL <= REGEXP_COUNT(t1.text, '([^'||chr(10)||'])+')
)
AS SYS.odciNumberList
)
) t2
)
GROUP BY ID;
'\n' - new line character will be saved as CHR(10) (ASCII value) in database. I used the same for conversion. Later used \n for display purpose.

split string into multiple rows with multiple columns

I have a string with user information. I have to write a function that takes string as input and inserts into some table.
Input string contains multiple rows with multiple columns like following:
inputString= "1,cassey,1222,12-12-12:2,timon,,02-02-12:3,john,3333,03-03-12"
what i want is to create insert from this...
How it can be achieved?
Solution in a single Query as following:
But I replaced the ',,' with 'NULL'
inputString= "1,cassey,1222,12-12-12:2,timon,NULL,02-02-12:3,john,3333,03-03-12"
SELECT REGEXP_SUBSTR (REGEXP_SUBSTR (inputString, '[^:]+', 1, LEVEL), '[^,]+', 1, 1) AS Col1,
REGEXP_SUBSTR (REGEXP_SUBSTR (inputString, '[^:]+', 1, LEVEL), '[^,]+', 1, 2) AS Col2,
REGEXP_SUBSTR (REGEXP_SUBSTR (inputString, '[^:]+', 1, LEVEL), '[^,]+', 1, 3) AS Col3,
REGEXP_SUBSTR (REGEXP_SUBSTR (inputString, '[^:]+', 1, LEVEL), '[^,]+', 1, 4) AS Col4
FROM DUAL
CONNECT BY LEVEL <= LENGTH (REGEXP_REPLACE (inputString, '[^:]+')) + 1;
Result:
Col1 Col2 Col3 Col4
------ ------ ------ ------
1 cassey 1222 12-12-12
2 timon NULL 02-02-12
3 john 3333 03-03-12
3 rows selected.
Package Spec:
CREATE OR REPLACE PACKAGE string_conversion AS
TYPE string_test_tab IS TABLE OF VARCHAR2(2000);
TAB string_test_tab;
PROCEDURE string_convert(v_input IN VARCHAR2, v_output OUT string_test_tab);
END string_conversion;
/
Package Body:
CREATE OR REPLACE PACKAGE BODY string_conversion AS
PROCEDURE string_convert(v_input IN VARCHAR2, v_output OUT string_test_tab) IS
v_index NUMBER := 1;
v_index_comma NUMBER := 1;
TAB2 string_test_tab;
v_input_str VARCHAR2(2000):= v_input||',';
BEGIN
v_output := string_test_tab();
LOOP
v_index_comma := INSTR (v_input_str, ',',v_index);
EXIT WHEN v_index_comma = 0;
v_output.extend();
v_output(v_output.count):= SUBSTR(v_input_str, v_index, v_index_comma - v_index);
dbms_output.put_line(v_output(v_output.count));
v_index := v_index_comma + 1;
END LOOP;
END string_convert;
END string_conversion;
/
Testing:
DECLARE
v_out1 string_conversion.string_test_tab;
BEGIN
string_conversion.string_convert('a,b,c,d,e',v_out1);
FOR j IN v_out1.FIRST .. v_out1.LAST
LOOP
dbms_output.put_line(v_out1(j));
END LOOP;
END;
/
OUTPUT:
a
b
c
d
e
DECLARE
v_string VARCHAR2(20) := 'a,b,c,d,e';
v_val VARCHAR2(2000);
BEGIN
dbms_output.put_line('v_string = '||v_string);
LOOP
v_val := SUBSTR(v_string,1, INSTR(v_string, ',', 1)-1);
IF INSTR(v_string, ',', 1) = 0 THEN
v_val := v_string;
END IF;
dbms_output.put_line(v_val);
EXIT WHEN INSTR(v_string, ',', 1) = 0;
v_string := SUBSTR(v_string,INSTR(v_string, ',', 1)+1);
END LOOP;
END;

Resources