问题
I have a Column in a Database which contains multiple Values in one Column, which i need as different rows. The Column contains comma delimited parts but also a Part with comma in brackets. I don't need to split this parts. (Only split on commas which are NOT in brackets)
Versions
Oracle 11g
Example:
**ID | Kategory**
1 | "ATD 5(2830),ATO 4(510),EDI 1,EH A1,SCI 2,SS 1,STO-SE 1(oral, CNS, blood),STO-SE 2(oral, respiratory effects)"
This string i need as
- 1 => ATD 5(2830)
- 1 => ATO 4(510)
- 1 => EDI 1
- 1 => EH A1
- 1 => SCI 2
- 1 => SS 1
- 1 => STO-SE 1(oral,CNS, blood)
- 1 => STO-SE 2(oral, respiratory effects)
Parts like (oral, CNS, blood) which contains comma in brackets i don't need to split.
回答1:
You can use the regular expression (([^(]*?(\(.*?\))?)*)(,|$)
to match:
[^(]*?
Zero-or-more (but as few as possible) non-opening-bracket characters(\(.*?\))?
Then, optionally, an opening bracket and as few characters as possible until the closing bracket.( )*
Wrapped in a capturing group repeated zero-or-more times( )
Wrapped in a capturing group to be able to reference the entire matched item(,|$)
Followed by either a comma or the end-of-string.
Like this:
SQL Fiddle
Oracle 11g R2 Schema Setup:
CREATE TABLE table_name ( ID, Kategory ) AS
SELECT 1, 'ATD 5(2830),ATO 4(510),EDI 1,EH A1,SCI 2,SS 1,STO-SE 1(oral, CNS, blood),STO-SE 2(oral, respiratory effects)' FROM DUAL;
Query 1:
SELECT ID,
l.COLUMN_VALUE AS item,
REGEXP_SUBSTR(
Kategory,
'(([^(]*?(\(.*?\))?)*)(,|$)',
1,
l.COLUMN_VALUE,
NULL,
1
) AS value
FROM table_name t
CROSS JOIN
TABLE(
CAST(
MULTISET(
SELECT LEVEL
FROM DUAL
CONNECT BY LEVEL < REGEXP_COUNT( t.Kategory, '(([^(]*?(\(.*?\))?)*)(,|$)' )
)
AS SYS.ODCINUMBERLIST
)
) l
Results:
| ID | ITEM | VALUE |
|----|------|-------------------------------------|
| 1 | 1 | ATD 5(2830) |
| 1 | 2 | ATO 4(510) |
| 1 | 3 | EDI 1 |
| 1 | 4 | EH A1 |
| 1 | 5 | SCI 2 |
| 1 | 6 | SS 1 |
| 1 | 7 | STO-SE 1(oral, CNS, blood) |
| 1 | 8 | STO-SE 2(oral, respiratory effects) |
来源:https://stackoverflow.com/questions/48376315/split-columntext-to-rows-extract-delimiter-in-bracket-oracle-sql