Oracle将文本拆分为列
我搜索了论坛,但没有找到我想要的答案 我的数据库中有一个很长的varchar2。我需要把这个除以 示例:标题| 20140528 | 3407 所以,我写了一个这样的查询:Oracle将文本拆分为列,oracle,split,Oracle,Split,我搜索了论坛,但没有找到我想要的答案 我的数据库中有一个很长的varchar2。我需要把这个除以 示例:标题| 20140528 | 3407 所以,我写了一个这样的查询: select regexp_substr(datas,'[^|]+',1,1) as col_1, regexp_substr(datas,'[^|]+',1,2) as col_2, regexp_substr(datas,'[^|]+',1,3) as col_3 from temp_ger
select regexp_substr(datas,'[^|]+',1,1) as col_1,
regexp_substr(datas,'[^|]+',1,2) as col_2,
regexp_substr(datas,'[^|]+',1,3) as col_3
from temp_gerben
但是,第二个值可能为空。在这种情况下,该行如下所示:
标题| | 3407
但是同一个查询不能很好地处理这个问题。这很简单,忽略第二个字段,并将第三个字段放在它的位置
由于我需要为不同的报表拆分此数据,因此我需要它将该字段保持为空,而不是忽略。问题是正则表达式正在查找至少一个非字符,而表达式中没有。解决此问题的一种方法是添加这样的字符:
select regexp_substr(replace(datas, '||', '| |'), '[^|]+', 1, 1) as col_1,
regexp_substr(replace(datas, '||', '| |'), '[^|]+', 1, 2) as col_2,
regexp_substr(replace(datas, '||', '| |'), '[^|]+', 1, 3) as col_3
from (select 'HEADER|20140528|3407' as datas from dual union all
select 'HEADER||3407' from dual
) temp_gerben;
另一种方法是搜索定界字符,使用*
而不是+
。那不太管用,因为计数开始了。您可以通过在字符串末尾出现一个
,查找以
结尾的图案,然后删除
,使其正常工作:
select replace(regexp_substr(datas || '|', '[^|]*[|]', 1, 1), '|', '') as col_1,
replace(regexp_substr(datas || '|', '[^|]*[|]', 1, 2), '|', '') as col_2,
replace(regexp_substr(datas || '|', '[^|]*[|]', 1, 3), '|', '') as col_3
from (select 'HEADER|20140528|3407' as datas from dual union all
select 'HEADER||3407' from dual
) temp_gerben;
非常感谢。我最终使用了你的第一个解决方案。我在一个过程中使用它,所以我只是用空值替换“”值。