|
Replies:
13
-
Pages:
1
-
Last Post:
Oct 5, 2007 6:51 PM
Last Post By: Buzzer
|
|
|
Posts:
27
Registered:
02/22/07
|
|
|
|
Does anyone have some wizzy code or bright ideas?
Posted:
Sep 28, 2007 7:49 AM
|
|
|
|
I have a string variable - varchar2(240) maximum - which is arbitarily filled with 3 letter codes. Any one or more of these codes may be duplicated within the string. What I need to do is to strip out all the duplications and end up with a single contiguous string containing all the unique code options. e.g.
Initial string 'AAABBBCCCBBBDDDAAAEEEBBB'
should end up as :-
'AAABBBCCCDDDEEE'
Hope this makes sense - also there is no need to place the unique codes in any kind of order.
Thanks for your help...
|
|
|
Posts:
2,217
Registered:
04/18/99
|
|
|
|
Re: Does anyone have some wizzy code or bright ideas?
Posted:
Sep 28, 2007 7:53 AM
in response to: Buzzer
|
|
|
|
SQL or PL/SQL, version of Oracle?
|
|
|
Posts:
27
Registered:
02/22/07
|
|
|
|
Re: Does anyone have some wizzy code or bright ideas?
Posted:
Sep 28, 2007 7:54 AM
in response to: padders
|
|
|
|
|
|
Posts:
1,535
Registered:
04/03/02
|
|
|
|
Re: Does anyone have some wizzy code or bright ideas?
Posted:
Sep 28, 2007 7:55 AM
in response to: Buzzer
|
|
|
|
Unless somebody has such a routine you'll have to write it yourself. The general logic for one way would be
parse string into associative array indexed by 3-letter code
loop through associative array using first, next, and last methods to recreate string
Message was edited by:
riedelme
|
|
|
Posts:
2,217
Registered:
04/18/99
|
|
|
|
Re: Does anyone have some wizzy code or bright ideas?
Posted:
Sep 28, 2007 8:02 AM
in response to: riedelme
|
|
|
parse string into associative array indexed by 3-letter code
Nice. The other way that springs to mind is to parse into nested table then use SET collection method to eliminate duplicates.
|
|
|
Posts:
1,535
Registered:
04/03/02
|
|
|
|
Re: Does anyone have some wizzy code or bright ideas?
Posted:
Sep 28, 2007 8:03 AM
in response to: padders
|
|
|
|
< parse into nested table then use SET collection method to eliminate duplicates >
Better, since looping should be easier
|
|
|
Posts:
2,307
Registered:
02/12/02
|
|
|
|
Re: Does anyone have some wizzy code or bright ideas?
Posted:
Sep 28, 2007 8:10 AM
in response to: Buzzer
|
|
|
This may be a lengthier way, but it yields results(assuming you only have 3 same letter contiguous characters in your string)
xopmaster@XOPUAT> WITH mytab AS
2 (SELECT 'AAABBBCCCBBBDDDAAAEEEBBB' mycol FROM DUAL)
3 SELECT REVERSE(REPLACE(MAX(SYS_CONNECT_BY_PATH(mystr,'/')),'/','')) output_str
4 FROM
5 (
6 SELECT mystr, rownum rn FROM
7 (
8 SELECT distinct substr(mycol, ((level-1) * 3) + 1,3) mystr FROM mytab
9 CONNECT BY LEVEL <= LENGTH(mycol) / 3
10 )
11 )
12 connect by rn + 1 = prior rn
13 ;
OUTPUT_STR
AAABBBCCCDDDEEE
|
|
|
Posts:
4,849
Registered:
09/08/98
|
|
|
|
Re: Does anyone have some wizzy code or bright ideas?
Posted:
Sep 28, 2007 8:45 AM
in response to: Sundar M
|
|
|
And in 10g, regular expressions and MODEL clause can do the job:
WITH t AS (SELECT 'AAABBBCCCBBBDDDAAAEEEBBB' col1
FROM dual
)
SELECT c1
, c2
FROM t
MODEL
DIMENSION BY (0 dim)
MEASURES(col1 c1, CAST ('' AS VARCHAR2(255)) c2, CAST ('x' AS VARCHAR2(255)) c3)
RULES ITERATE(99) UNTIL (c3[0] IS NULL)
(c3[0] = REGEXP_SUBSTR(c1[0], '([A-Z])\1{2}', 1, ITERATION_NUMBER+1)
,c2[0] = c2[0] || CASE WHEN NVL(INSTR(c2[0], c3[0]), 0) = 0 THEN c3[0] END
)
;
C1 C2
------------------------------ ------------------------------
AAABBBCCCBBBDDDAAAEEEBBB AAABBBCCCDDDEEE
C.
|
|
|
Posts:
986
Registered:
04/14/07
|
|
|
|
Re: Does anyone have some wizzy code or bright ideas?
Posted:
Sep 28, 2007 1:29 PM
in response to: cd_2
|
|
|
And in 10g, regular expressions and MODEL clause can
do the job:
another way to do it with MODEL (less iterations, less measures):
SQL> WITH t AS (SELECT 'AAABBBCCCBBBDDDAAAEEEBBB' str
2 FROM dual
3 )
4 select str from t
5 model
6 dimension by (0 dim)
7 measures(str)
8 rules iterate(100) until (str[0] = previous(str[0]))
9 (str[0]=regexp_replace(str[0],'(([[:alpha:]])\2{2})(.*?)\1','\1\3'));
STR
------------------------
AAABBBCCCDDDEEE
SQL>
|
|
|
Posts:
4,849
Registered:
09/08/98
|
|
|
|
Re: Does anyone have some wizzy code or bright ideas?
Posted:
Sep 28, 2007 1:56 PM
in response to: Volder
|
|
|
|
|
|
Posts:
539
Registered:
03/08/06
|
|
|
|
Re: Does anyone have some wizzy code or bright ideas?
Posted:
Sep 28, 2007 10:32 PM
in response to: Buzzer
|
|
|
before
AAABBBCCCBBBDDDAAAEEEBBB
after
AAABBBCCCDDDEEE
If you can use Editor,
many times
replace
((.)(?=(\2+))\3((?!\2).)+)\2+
to
\1
If you can use JavaStoredProcedure,
many times
replace
((.)\2++((?!\2).)+)\2+
to
\1
Possessive quantifiers
http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html
|
|
|
Posts:
539
Registered:
03/08/06
|
|
|
|
Re: Does anyone have some wizzy code or bright ideas?
Posted:
Sep 28, 2007 10:56 PM
in response to: Aketi Jyuuzou
|
|
|
oops
I took mistake.
We do not need Possessive quantifiers.
many times
replace
((.)\2+((?!\2).)+)\2+
to
\1
|
|
|
Posts:
539
Registered:
03/08/06
|
|
|
|
Re: Does anyone have some wizzy code or bright ideas?
Posted:
Sep 28, 2007 11:27 PM
in response to: Aketi Jyuuzou
|
|
|
sorry
I took more mistake.
before
AAABBBCCCBBBDDDAAAEEEBBB
after
AAABBBCCCDDDEEE
before
AAABBBCCCCCCDDDDDDEEEDDDEEEDDD
after
AAABBBCCCDDDEEE
many times
replace
((.)\2{2}(.*?))\2+
to
\1
|
|
|
Posts:
27
Registered:
02/22/07
|
|
|
|
Re: Does anyone have some wizzy code or bright ideas?
Posted:
Oct 1, 2007 3:23 AM
in response to: Aketi Jyuuzou
|
|
|
|
Many thanks for everyones help with this... not only do I now have a slick solution - but it looks like I know what I'm doing!!
Greatly appreciated.
|
|
|
|
Legend
|
|
Guru : 2500
- 1000000
pts
|
|
Expert : 1000
- 2499
pts
|
|
Pro : 500
- 999
pts
|
|
Journeyman : 200
- 499
pts
|
|
Newbie : 0
- 199
pts
|
|
Oracle ACE Director
|
|
Oracle ACE Member
|
|
Oracle Employee ACE
|
|
Helpful Answer
(5 pts)
|
|
Correct Answer
(10 pts)
|
|