Below script can be used to remove the repeating (Duplicate String ) String from a string and return unique string Concatenated by comma (replace comma in below example with any string separator)
FUNCTION remove_duplicate_strings( p_string IN VARCHAR2)
RETURN VARCHAR2
IS
l_temp_string VARCHAR2(4000);
l_loop_cnt NUMBER := 0;
l_string VARCHAR2(4000);
l_final_sting VARCHAR2(4000) := ' ';
TYPE string_tbl_typ IS TABLE OF VARCHAR2(4000)
INDEX BY VARCHAR(4000);
string_tbl string_tbl_typ;
BEGIN
l_temp_string :=
REGEXP_REPLACE(
p_string
, '[,]+'
, ',');
LOOP
l_loop_cnt := l_loop_cnt + 1;
l_string :=
REGEXP_SUBSTR(
p_string
, '[^,]+'
, 1
, l_loop_cnt);
EXIT WHEN l_string IS NULL;
IF NOT string_tbl.EXISTS( l_string)
THEN
string_tbl( l_string) := l_string;
l_final_sting :=
l_final_sting
|| ','
|| l_string;
END IF;
END LOOP;
l_final_sting := TRIM( BOTH ',' FROM TRIM( l_final_sting));
RETURN l_final_sting;
EXCEPTION
WHEN OTHERS
THEN
DBMS_OUTPUT.put_line(
SQLERRM
|| CHR( 10)
|| DBMS_UTILITY.format_error_backtrace);
RETURN p_string;
END;
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.