Oracle Database 11g R2 11.2.0.1 SE1
考虑下表有3行的地址
ROW1 :aaaaaaaaaaaaaaaaaaaaaaaaaaCRLFbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbCRLFcccccccccccccccCRLFddddd
ROW2 :eeeeCRLFffffffffffffgggggggghCRLF
ROW3 :iiCRLFjjjjjjjjjjkkkCRLF
我需要根据以下条件分割字符串
条件1 :将字符串拆分为CRLF
条件2 :如果拆分字符串的长度大于10,则将该字符串拆分为10个字符的长度。
注意:数据的长度不固定。
结果: -
的 ADDRESS
ROW1 :aaaaaaaaaa
AAAAAAAAAA
aaaaaCRLF
bbbbbbbbbb
bbbbbbbbbb
bbbbbbbbbb
bbCRLF
CCCCCCCCCC
cccccCRLF
DDDDD
ROW2 :eeeeCRLF
ffffffffff
ffgggggggg
hCRLF
ROW3 :iiCRLF
jjjjjjjjjj
kkkCRLF
答案 0 :(得分:1)
此答案基于此处的技术:https://stackoverflow.com/a/29338715/4311
首先,我们将在名为data
的CTE中设置您的样本数据。然后,查询根据CRLF的出现将此文本拆分为行,这些行为给出了data2
。
然后我们在结果字符串中每10个字符添加一个CRLF,这样就可以得到data3
。最后,我们再次重复原始流程,根据CRLF将文本拆分为行,产生data4
。
with data as
(select 'row1' rowval, 'aaaaaaaaaaaaaaaaaaaaaaaaaa' || chr(13) || chr(10) || 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb' || chr(13) || chr(10) || 'ccccccccccccccc' || chr(13) || chr(10) || 'ddddd' txt from dual union all
select 'row2' rowval, 'eeee' || chr(13) || chr(10) || 'ffffffffffffggggggggh' || chr(13) || chr(10) txt from dual union all
select 'row3' rowval, 'ii' || chr(13) || chr(10) || 'jjjjjjjjjjkkk' || chr(13) || chr(10) txt from dual
)
select rowval,
txt
from (select rowval,
trim(regexp_substr(txt, '[^' || CHR(13)||CHR(10) || ']+', 1, lines.column_value)) txt
from (select rowval,
regexp_replace(txt,
'(.{10})',
'\1' || CHR(13) || CHR(10)) txt
from (SELECT rowval,
trim(regexp_substr(data.txt, '[^' || CHR(13)||CHR(10) || ']+', 1, lines.column_value)) txt
FROM data,
TABLE(CAST(MULTISET(SELECT LEVEL
FROM dual
CONNECT BY instr(data.txt, CHR(13)||CHR(10), 1, LEVEL - 1) > 0
) AS sys.odciNumberList
)
) lines
) data2
) data3,
TABLE(CAST(MULTISET(SELECT LEVEL
FROM dual
CONNECT BY instr(data3.txt, CHR(13)||CHR(10), 1, LEVEL - 1) > 0
) AS sys.odciNumberList
)
) lines
) data4
where txt is not null
ORDER BY rowval, txt;
此查询产生我们:
ROWV TXT
---- ------------
row1 aaaaaa
row1 aaaaaaaaaa
row1 aaaaaaaaaa
row1 bb
row1 bbbbbbbbbb
row1 bbbbbbbbbb
row1 bbbbbbbbbb
row1 ccccc
row1 cccccccccc
row1 ddddd
row2 eeee
row2 ffffffffff
row2 ffgggggggg
row2 h
row3 ii
row3 jjjjjjjjjj
row3 kkk