oracle 在 pl/sql 中标记一个字符串并获得唯一的标记?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19187404/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 01:59:00  来源:igfitidea点击:

Tokenize a string in pl/sql and get unique tokens?

sqloracleplsqltokentokenize

提问by kenteer

I've a requirement to tokenize a string in pl/sql and return only unique tokens. I've seen examples that will tokenize the string, but none that will return unique tokens.

我需要在 pl/sql 中标记一个字符串并只返回唯一的标记。我见过将字符串标记化的示例,但没有一个会返回唯一标记的示例。

For example the query -

例如查询 -

select tokenize('hi you person person', ' ') as col1 from dual;

should return TOKEN_LIST('hi','you','person')

应该回来 TOKEN_LIST('hi','you','person')

instead of TOKEN_LIST('hi','you','person','person')

代替 TOKEN_LIST('hi','you','person','person')

回答by PM 77-1

with t as (select 'aaaa bbbb cccc dddd eeee ffff aaaa' as txt from dual)
-- end of sample data
select DISTINCT REGEXP_SUBSTR (txt, '[^[:space:]]+', 1, level) as word
from t
connect by level <= length(regexp_replace(txt,'[^[:space:]]+'))+1;

The above script produces the following result:

上述脚本产生以下结果:

WORD
dddd
eeee
bbbb
ffff
cccc
aaaa

The idea is shamelessly stolen from OTN Community answer.

这个想法是从OTN 社区答案中无耻地窃取的。

SQL Fiddle

SQL小提琴