我需要在 Oracle 中编写脚本,在查询中按 / 字符拆分字符串“BP/593/00294”和“NC//12345”,以便在单独的列中包含值。
我在想这样的事情:
select regexp_substr(mystr, '[^/]+') as col1,
regexp_substr(c.usertext21,'[^/]+',1,2) as col2
from mytable
但在 col2 中,我从第二个字符串中丢失了空字符串值。
我需要保留两个字符串中的每个值。这应该是结果:
<table>
<th>col1</th>
<th>col2</th>
<th>col3</th>
<tr>
<td>BP</td>
<td>593</td>
<td>00294</td>
</tr>
<tr>
<td>NC</td>
<td></td>
<td>12345</td>
</tr>
</table>
任何帮助,将不胜感激。
谢谢
您可以捕获 0 个或更多字符,除了/
在字符串的开头或之后/
:
select
regexp_substr('BP/593/00294', '(^|/)([^/]*)') as col1,
regexp_substr('BP/593/00294', '(^|/)([^/]*)', 1, 2, null, 2) as col2,
regexp_substr('BP/593/00294', '(^|/)([^/]*)', 1, 3, null, 2) as col3
from dual
See the 在线演示.
Details
-
(^|/)
- 捕获组 1:字符串开头或/
-
([^/]*)
- 捕获组 2:任何 0 个或更多字符,除了/
.
请注意2
提取第 2 组值的参数。请参阅正则表达式演示.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)