Oracle PL/SQL 中有分割字符串的函数吗?
我需要编写一个过程来规范化具有由一个字符连接的多个标记的记录。我需要获取这些标记来分割字符串,并将每个标记作为新记录插入表中。 Oracle有类似“split”的功能吗?
I need to write a procedure to normalize a record that have multiple tokens concatenated by one char. I need to obtain these tokens splitting the string and insert each one as a new record in a table. Does Oracle have something like a "split" function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
有
apex_util.string_to_table
- 请参阅我对此的回答 问题。另外,在上述功能存在之前,我曾经发布过一个解决方案 在我的博客上。
更新
在 APEX 的更高版本中,
apex_util.string_to_table
已已弃用,以及类似的函数首选 apex_string.split。There is
apex_util.string_to_table
- see my answer to this question.Also, prior to the existence of the above function, I once posted a solution here on my blog.
Update
In later versions of APEX,
apex_util.string_to_table
is deprecated, and a similar function apex_string.split is preferred.如果
APEX_UTIL
不可用,您可以使用REGEXP_SUBSTR()
解决方案。灵感来自 http://nuijten.blogspot.fr/2009/07/splitting-逗号分隔字符串-regexp.html:
If
APEX_UTIL
is not available, you have a solution usingREGEXP_SUBSTR()
.Inspired from http://nuijten.blogspot.fr/2009/07/splitting-comma-delimited-string-regexp.html :
你必须自己动手。例如,
You have to roll your own. E.g.,
您可以使用 regexp_substr()。示例:
查询迭代逗号分隔的字符串,搜索逗号 (,),然后通过将逗号视为分隔符来分割字符串。每当遇到分隔符时,它都会将字符串作为一行返回。
语句
regexp_substr('SMITH,ALLEN,WARD,JONES','[^,]+', 1, level)
中的level
指的是Oracle中使用的伪列在分层查询中以数字格式标识层次结构级别:连接方式中的级别You can use regexp_substr(). Example:
The query iterates through the comma separated string, searches for the comma (,) and then splits the string by treating the comma as delimiter. It returns the string as a row, whenever it hits a delimiter.
level
in statementregexp_substr('SMITH,ALLEN,WARD,JONES','[^,]+', 1, level)
refers to a pseudocolumn in Oracle which is used in a hierarchical query to identify the hierarchy level in numeric format: level in connect by这仅适用于 Oracle 10G 及更高版本。
基本上,您使用 regex_substr 对字符串进行分割。
:
已存档链接: http://web. archive.org/web/20170304121704/https://blogs.oracle.com/aramamoo/entry/how_to_split_comma_separated_string_and_pass_to_in_clause_of_select_statement
代码:
This only works in Oracle 10G and greater.
Basically, you use regex_substr to do a split on the string.
https://blogs.oracle.com/aramamoo/entry/how_to_split_comma_separated_string_and_pass_to_in_clause_of_select_statement
Edit:
Archived link: http://web.archive.org/web/20170304121704/https://blogs.oracle.com/aramamoo/entry/how_to_split_comma_separated_string_and_pass_to_in_clause_of_select_statement
The code:
您可以使用 SUBSTR 和 INSTR 的组合,如下所示:
示例字符串:
field = 'DE124028#@$1048708#@$000#@$536967136#@$'
分隔符为 #@$。
例如,要获取“1048708”:
如果该字段具有固定长度(此处为 7):
如果该字段具有可变长度:
您可能应该研究 SUBSTR 和 INSTR 函数以获得更大的灵活性。
,1,2) - (instr(field,'#@您可能应该研究 SUBSTR 和 INSTR 函数以获得更大的灵活性。
,1,1)+3,7)如果该字段具有可变长度:
您可能应该研究 SUBSTR 和 INSTR 函数以获得更大的灵活性。
,1,1)+3))您可能应该研究 SUBSTR 和 INSTR 函数以获得更大的灵活性。
,1,1)+3,7)如果该字段具有可变长度:
您可能应该研究 SUBSTR 和 INSTR 函数以获得更大的灵活性。
You could use a combination of SUBSTR and INSTR as follows :
Example string :
field = 'DE124028#@$1048708#@$000#@$536967136#@$'
The seperator being #@$.
To get the '1048708' for example :
If the field is of fixed length ( 7 here ) :
If the field is of variable length :
You should probably look into SUBSTR and INSTR functions for more flexibility.
,1,2) - (instr(field,'#@You should probably look into SUBSTR and INSTR functions for more flexibility.
,1,1)+3,7)If the field is of variable length :
You should probably look into SUBSTR and INSTR functions for more flexibility.
,1,1)+3))You should probably look into SUBSTR and INSTR functions for more flexibility.
,1,1)+3,7)If the field is of variable length :
You should probably look into SUBSTR and INSTR functions for more flexibility.
请查找下一个您可能会发现有用的示例
--1st 子串
--2nd 子串
--3rd 子串
--4th 子串
最好的问候
Emanuele
Please find next an example you may find useful
--1st substring
--2nd substring
--3rd substring
--4th substring
Best regards
Emanuele
在 Oracle 中,下面的 SQL 会将 myString 拆分为子字符串:
结果是:
In Oracle, below SQL will split myString to substring:
Result is:
我需要一个函数来分割 clob 并确保该函数在 sql 中可用。
测试:
I needed a function that splits a clob and makes sure the function is usable in sql.
Test:
我喜欢那个顶级实用程序的外观。不过,了解可用于此目的的标准 Oracle 函数也是有好处的:subStr 和 inStr
http://download.oracle.com/docs/cd /B19306_01/server.102/b14200/functions001.htm
I like the look of that apex utility. However its also good to know about the standard oracle functions you can use for this: subStr and inStr
http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/functions001.htm
大家有一个简单的方法。使用替换功能。以下是准备传递给 IN 子句的逗号分隔字符串的示例。
在 PL/SQL 中:
在 SQL Plus 中:
There is a simple way folks. Use REPLACE function. Here is an example of comma separated string ready to be passed to IN clause.
In PL/SQL:
In SQL Plus: