在 PL/SQL 中将逗号分隔的字符串转换为数组
如何将逗号分隔的字符串转换为数组?
我有输入 '1,2,3'
,我需要将其转换为数组。
How do I convert a comma separated string to a array?
I have the input '1,2,3'
, and I need to convert it into an array.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
我们永远不会用尽不同的选择来做同样的事情,对吗?
我最近发现这非常方便:
输出:
We can never run out of alternatives of doing the same thing differently, right?
I recently found this is pretty handy:
Outputs:
我知道 Stack Overflow 不赞成在没有解释的情况下粘贴 URL,但这个特定页面有一些非常好的选项:
http://www. oratechinfo.co.uk/delimited_lists_to_collections.html
我特别喜欢这个,它将分隔列表转换为可以运行查询的临时表:
I know Stack Overflow frowns on pasting URLs without explanations, but this particular page has a few really good options:
http://www.oratechinfo.co.uk/delimited_lists_to_collections.html
I particularly like this one, which converts the delimited list into a temporary table you can run queries against:
简单的代码
Simple Code
是的,令人沮丧的是 dbms_utility.comma_to_table 仅支持逗号分隔列表,并且仅当列表中的元素是有效的 PL/SQL 标识时(因此数字会导致错误)。
我创建了一个通用解析包,可以满足您的需要(粘贴在下面)。它是我的“demo.zip”文件的一部分,该文件是一个包含 2000 多个支持我的培训材料的文件的存储库,所有这些文件都可以在 PL/SQL Obsession 上获得:www.toadworld.com/SF。
问候,
史蒂文·福尔斯坦
www.plsqlchallenge.com
(每日 PL/SQL 测验)
Yes, it is very frustrating that dbms_utility.comma_to_table only supports comma delimieted lists and then only when elements in the list are valid PL/SQL identifies (so numbers cause an error).
I have created a generic parsing package that will do what you need (pasted below). It is part of my "demo.zip" file, a repository of over 2000 files that support my training materials, all available at PL/SQL Obsession: www.toadworld.com/SF.
Regards,
Steven Feuerstein
www.plsqlchallenge.com
(daily PL/SQL quiz)
使用 流水线表函数:
让我们看看输出:
Using a pipelined table function:
Let's see the output:
我只是想把理查德和迈克的答案写得更清楚。
方法 1:VARRAY USAGE
但在此方法中您必须声明数组大小。如果您想要更动态的方法,您可以使用迈克的方法。
方法二:
I just want to write more clearly Richard and Mike's answers.
Method 1: VARRAY USAGE
But in this method you must declare array size. If you want more dynamic method you could use Mike's method.
METHOD 2:
在 BBDD 上快速搜索后,我找到了一个名为 split 的函数:
我不知道它是否有用,但我们找到了它 此处...
A quick search on my BBDD took me to a function called split:
I don't know if it'll be of use, but we found it here...
我正在寻找类似的解决方案,其中在逗号分隔的列表中包含多字节字符(连字符、空格、下划线)。所以 dbms_utility.comma_to_table 不适合我。
这不是一个专家的答案,所以我希望有人能改进这个答案。
I was looking for a similar solution where I had multi-byte characters (hyphen, whitespace, underscore) in comma separated lists. So
dbms_utility.comma_to_table
didn't work for me.This is not an expert answer so I hope someone would improve this answer.
你可以使用oracle方法
you can use oracle method
另一种可能性是:
运行起来有点太昂贵,因为每次调用者请求其中的一项时,它都会计算完整的分割......
Another possibility is:
It's a little too expensive to run, as it computes the complete split every time the caller asks for one of the items in there...
Stewart Ashton 在这个链接中提出的解决方案非常方便。
他消除了值列表必须是整数的需要,因此您可以使用字符串列表。
在WITH 子句中,他用单引号将值括起来,然后将其转换为具有单个VARCHAR2 类型列的表。
https://stewashton.wordpress.com
The solution presented below by Stewart Ashton in this link is pretty handy.
He eliminates the need for the value list to be integer, so you can use string list.
In the WITH clause, he surrounds the values with single quotes then converts it to a table having a single column of type VARCHAR2.
https://stewashton.wordpress.com
因此,仅在 for 循环中选择查询可以通过将 dosweeklist 替换为分隔字符串并将分隔符替换为分隔字符来实现此目的。
让我们看看输出
so select query only in for loop can do the trick, by replacing dosweeklist as your delimited string and seprator as your delimited character.
Lets see output
您可以使用替换功能轻松替换逗号。
要执行此操作 -
SQL Server (Transact-SQL) 中 REPLACE 函数的语法为:
REPLACE( string, string_to_replace, replacement_string )
参数或参数
string : 源字符串,其中的字符序列将被另一组字符替换字符。
string_to_replace: 将在 string1 中搜索的字符串。
replacement_string:替换字符串。所有出现的 string_to_replace 都将被替换为 string1 中的 replacement_string。
注意:
例如:
从 DUAL 中选择 REPLACE('Kapil,raj,chouhan', ',' , ' ');
结果:Kapil raj chouhan
SELECT REPLACE('I Live In India', ' ' , '-') from DUAL;
结果:I-Live-In-India
SELECT REPLACE('facebook.com', 'face' , 'friends') from DUAL;
结果:friendsbook.com
我希望它对您有用。
You can use Replace Function to replace comma easily.
To Do this-
The syntax for the REPLACE function in SQL Server (Transact-SQL) is:
REPLACE( string, string_to_replace, replacement_string )
Parameters or Arguments
string : The source string from which a sequence of characters will be replaced by another set of characters.
string_to_replace : The string that will be searched for in string1.
replacement_string : The replacement string. All occurrences of string_to_replace will be replaced with replacement_string in string1.
Note :
For Example :
SELECT REPLACE('Kapil,raj,chouhan', ',' , ' ') from DUAL;
Result : Kapil raj chouhan
SELECT REPLACE('I Live In India', ' ' , '-') from DUAL;
Result : I-Live-In-India
SELECT REPLACE('facebook.com', 'face' , 'friends') from DUAL;
Result : friendsbook.com
I Hope it will be usefull for you.
这是另一个更简单的选择
here is another easier option
Oracle提供了内置函数DBMS_UTILITY.COMMA_TO_TABLE。
不幸的是,这个不适用于数字:
但是通过一个小技巧在元素前面加上“x”,它就可以工作:
问候,
抢。
Oracle provides the builtin function DBMS_UTILITY.COMMA_TO_TABLE.
Unfortunately, this one doesn't work with numbers:
But with a little trick to prefix the elements with an 'x', it works:
Regards,
Rob.