[英]Oracle 11gR2: split string with multiple delimiters(add)
String val = "ABC,abc|DEF,def|GHI,g hi|JKL,jkl";
How to split this string and insert into tables using sql, pl/sql or whatever.如何使用 sql、pl/sql 或其他方法拆分此字符串并插入表中。
,
is column delimiter and |
,
是列分隔符和|
is row delimiter.是行分隔符。
is it possible?是否可以?
[expected result] [预期结果]
col1 col2
------------
ABC abc
DEF def
GHI g hi
JKL jkl
Thanks reply.谢谢回复。 I have a another question.我还有一个问题。
this is a string.这是一个字符串。
String val = "ABC,abc||D|@EF,def||G|HI,g hi||JKL,jkl";
I want to split by only ||
我只想按||
delimiter.分隔符。 how to use the RegExp?如何使用正则表达式?
I try it such as '[^|]{2}+'
, '^[|]{2}+', 1,
etc..我尝试诸如'[^|]{2}+'
、 '^[|]{2}+', 1,
等。
this is my wrong result.这是我的错误结果。
[wrong result] [错误结果]
COL1 COL2
---------- ----------
BC abc
D
@EF def
G
HI g hi
JKL jkl
[expected result] [预期结果]
col1 col2
-----------
ABC abc
D|@EF def
G|HI g hi
JKL jkl
Try this.尝试这个。 It will help you out.它会帮助你。
SELECT SUBSTR(A.tx,1,instr(a.tx,',',1)-1) col1,
SUBSTR(A.tx,instr(a.tx,',',1) +1,LENGTH(a.tx)) col2
FROM
(SELECT TRIM(regexp_substr(REPLACE('ABC,abc||D|@EF,def||G|HI,g hi||JKL,jkl','||','$'),'[^$]+', 1, level)) tx
FROM dual
CONNECT BY regexp_substr(REPLACE('ABC,abc||D|@EF,def||G|HI,g hi||JKL,jkl','||','$'), '[^$]+', 1, level) IS NOT NULL
)A;
Try this.尝试这个。 Hope this helps.希望这可以帮助。
SELECT
REGEXP_SUBSTR (str, '[^,]+', 1, 1) AS COL1
,REGEXP_SUBSTR (str, '[^,]+', 1, 2) AS COL2
from (
select
trim(regexp_substr('ABC,abc|DEF,def|GHI,ghi|JKL,jkl', '[^|]+', 1, LEVEL)) str
FROM
DUAL
CONNECT BY instr('ABC,abc|DEF,def|GHI,ghi|JKL,jkl', '|', 1, LEVEL - 1) > 0
)
We can use CTE(Common Table Expressions). For the first part of your query try this:
with cte(i,strr1,strr2) as
(
select 1 as i,regexp_substr('ABC,abc|DEF,def|GHI,g hi|JKL,jkl','[A-Z]+',1,1) as strr1,regexp_substr('ABC,abc|DEF,def|GHI,g hi|JKL,jkl','[a-z]+(\s)*[a-z]+|[a-z]+(\s)*[a-z]+|',1,1) as strr2 from dual
union all
select i+1,regexp_substr('ABC,abc|DEF,def|GHI,g hi|JKL,jkl','[A-Z]+',1,i+1) as strr1,regexp_substr('ABC,abc|DEF,def|GHI,g hi|JKL,jkl','[a-z]+(\s)*[a-z]+|[a-z]+(\s)*[a-z]+|',1,i+1) as strr2 from cte where i+1<5
)select strr1,strr2 from cte;
Here's a different approach that handles nulls and does not risk altering any data.这是一种不同的方法,可以处理空值并且不会冒更改任何数据的风险。 It uses a with clause to break down the data by the delimiters, ending up with splitting on the commas.它使用 with 子句按分隔符分解数据,最后以逗号分隔。
ASSUMPTION: Steps have already been taken to ensure that the data has already been scrubbed of the delimiter characters (commas and pipes).假设:已经采取措施确保数据已经清除了分隔符(逗号和管道)。
-- Original data with multiple delimiters and a NULL element for testing.
with orig_data(str) as (
select 'ABC,abc||||G|HI,g hi||JKL,jkl' from dual
),
--Split on first delimiter (double-pipes)
Parsed_data(rec) as (
select regexp_substr(str, '(.*?)(\|\||$)', 1, LEVEL, NULL, 1)
from orig_data
CONNECT BY LEVEL <= REGEXP_COUNT(str, '\|\|') + 1
)
-- For testing-shows records based on 1st level delimiter
--select rec from parsed_data;
-- Split the record into columns
select regexp_replace(rec, '^(.*),.*', '\1') col1,
regexp_replace(rec, '^.*,(.*)', '\1') col2
from Parsed_data;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.