How can I get unique characters from a string in Oracle?

How to get unique characters from a string in Oracle?

Let's say I have a data column

CCCXXXCCCCCCCCCCCCCCCCXXCCCCCCCCCCCCCCCCCCCCCCC 

and I want to return 'CX' to the list of unique characters.

Similarly: aaaabbbccccdddaaa ==> abcd, HelloO ==> HeloO

The order is important, I want them to be in the same order in which they first appear on the line.

Is there a way to do this without using a stored procedure?

Edit: add some examples

+4
source share
1 answer
 SELECT SUBSTR(REGEXP_SUBSTR('CCCCXXXCCCCCCCCCCCCCCCCXXCCCCCCCCCCCCCCCCCCCCCCC', '^(.)\1*.'), -2, 2) RESULT FROM DUAL; 

It returns CX

Here is another solution:

 Select Replace (Wm_Concat (C), ',', '') From (Select Substr ('CCCXCCCXXXCCCCCCCCCCCCCCCCXXCCCCCCCCCCCCCCCC', Rownum, 1) C, Min (Rownum) Rn From Dual Connect By Rownum <= Length ( 'CCCXCCCXXXCCCCCCCCCCCCCCCCXXCCCCCCCCCCCCCCCC') Group By Substr ( 'CCCXCCCXXXCCCCCCCCCCCCCCCCXXCCCCCCCCCCCCCCCC', Rownum, 1) Order By Rn ) X; 

It returns all unique characters in the order they appear. By the way, yes it looks awful

+10
source

Source: https://habr.com/ru/post/1369431/


All Articles