Hello Wkinoue,
Welcome to the Glufke Forum.
For the solution of your problem, let's go to parts:
First, we will first be removed from string all that does not interest us, that is, all Characters other than "numbers" or "pipe". To do this, we can use the "regExp_Replace" function with the filter "[^ 0-9 \ |]", where we only keep the numerical digits and the pipe. Run the queries below:
Select all
SQL> SELECT
'[LOG]2331|1321{VP}|4524|5512]|885|4242113234{eddx]|5424|4524' MINHA_STRING,
REGEXP_REPLACE('[LOG]2331|1321{VP}|4524|5512]|885|4242113234{eddx]|5424|4524', '[^0-9\|]','') MINHA_STRING_FILTRADA
FROM DUAL
SQL>
MINHA_STRING MINHA_STRING_FILTRADA
------------------------------------------------------------ ------------------------------------------
[LOG]2331|1321{VP}|4524|5512]|885|4242113234{eddx]|5424|4524 2331|1321|4524|5512|885|4242113234|5424|4524
Ready! Note that you have a line only with numeric values, separated by pipe:
2331|1321|4524|5512|885|4242113234|5424|4524
Now, let's try to identify how many values ??exist on the string. You said the official separator would be the "pipe". So let's repeat the regexp_replace command to identify how many pipes there are on the string. In this case, I will use the filter "[^ |]" to keep the PIPE of the initial string:
Select all
SQL> SELECT REGEXP_REPLACE('2331|1321|4524|5512|885|4242113234|5424|4524', '[^|]','') TOTAL
FROM DUAL;
TOTAL
-------
|||||||
But it is very boring to tell the resulting "chopsticks" of the quieu. We will improve the same with the help of the Length and Trim commands:
Select all
SQL> SELECT LENGTH(TRIM(REGEXP_REPLACE('2331|1321|4524|5512|885|4242113234|5424|4524', '[^|]',''))) TOTAL
2 FROM DUAL;
TOTAL
----------
7
Ok .. So until here we already have the "purified string" and how many numbers They meet on the string, based on the counts of the "Pipe" tabs.
For this, we need the "regexp_substr ... Connect by Level ..." command. Run this desire now
Select all
SQL> SELECT trim(regexp_substr('2331|1321|4524|5512|885|4242113234|5424|4524', '[^|]+', 1, LEVEL)) RESULTADO
2 FROM dual
3 CONNECT BY LEVEL <= 7+1;
RESULTADO
--------------------------------------------------------------------------------
2331
1321
4524
5512
885
4242113234
5424
4524
Some explanations:
- Note in the regExp_subtr command that I have informed the second parameter as' [^ |] + '. This indicates for it that this is the value separator.
- Note at the level I added 1 to the value (EX: 7 + 1). The reason is that the separators are always a number less than the total number of records.
- Note that I used Length and RegExp_Replace to identify how many tabs exist. At 10g this is how to do this. In 11g I step to use the regexp_count
well .. now let's check which distribution of these values ??in string. Let's put the previous cradle inside a
SELECT .. COUNT(*) FROM .. GROUP BY
Run now:
Select all
SQL> SELECT RESULTADO,COUNT(*)
2 FROM
3 (
4 SELECT trim(regexp_substr('2331|1321|4524|5512|885|4242113234|5424|4524', '[^|]+', 1, LEVEL)) RESULTADO
5 FROM dual
6 CONNECT BY LEVEL <= 7+1
7 )
8 GROUP BY RESULTADO;
RESULTADO COUNT(*)
---------- ----------
4524 2
1321 1
5424 1
5512 1
2331 1
885 1
4242113234 1
We then finish counting That you needed to do. I will now repeat this querie by adding in her everything we explained earlier:
Select all
SQL> SELECT RESULTADO,COUNT(*)
2 FROM
3 (
4 SELECT trim(regexp_substr(REGEXP_REPLACE('[LOG]2331|1321{VP}|4524|5512]|885|4242113234{eddx]|5424|4524', '[^0-9\|]',''), '[^|]+', 1, LEVEL)) RESULTADO
5 FROM dual
6 CONNECT BY LEVEL <= LENGTH(TRIM(REGEXP_REPLACE('[LOG]2331|1321{VP}|4524|5512]|885|4242113234{eddx]|5424|4524', '[^|]','')))+1
7 )
8 GROUP BY RESULTADO
9 /
RESULTADO COUNT(*)
---------- ----------
4524 2
1321 1
5424 1
5512 1
2331 1
885 1
4242113234 1
I developed this explanation through consultations to sites on the Internet, which I am posting here:
http://www.sqlsnippets.com/en/topic-12818.html http://stackoverflow.com/questions/1099 ... gits-again
I hope I have solved your problem, and thank you for posting this subject, since I had not encountered Still with this type of problem.
Hugs,
Sergio Coutinho