拆分字符串并在Like Command中使用

时间:2013-07-30 05:11:09

标签: sql oracle

我有一个像'from+google+world'

这样的字符串

我需要在任何地方搜索整个三个关键字的文本 对于我们正在使用的单个查询,

select * from tabl name 
where desc like '%from%' 
and desc like '%google%' 
and desc like '%world%'

如何拆分字符串并将其与上述查询一起使用。 @p1+@p2+@p3

select * from tabl name 
where desc like '%@p1%' 
and desc like '%@p2%' 
and desc like '%@p3%'

Radu Gheorghiu:这个命令有50%的帮助它只是用De-Limiter分割字符串我必须得到有3个分裂字的列,例如我们必须得到描述'所有@p3的东西是@ p1去@ p2罚款'和desc不喜欢'所有@p3的东西都是@ p1正常'

3 个答案:

答案 0 :(得分:2)

这是您需要的代码here is a SQLFiddle

with test as 
    (select 'from+google+world' str from dual  
    )  
    select regexp_substr (str, '[^+]+', 1, rownum) split  
    from test  
    connect by level <= length (regexp_replace (str, '[^+]+'))  + 1

答案 1 :(得分:1)

你的意思是这个输出吗?

from
google
world

WITH t AS (SELECT 'from+google+world' word FROM dual)
SELECT regexp_substr(word, '[[:alpha:]]+', 1, LEVEL) FROM T
CONNECT BY regexp_substr(word, '[[:alpha:]]+', 1, level) is not null;

你的意思是这个输出吗?

from
google
world

WITH t AS (SELECT 'from+google+world' word FROM dual)
SELECT regexp_substr(word, '[[:alpha:]]+', 1, LEVEL) FROM T
CONNECT BY regexp_substr(word, '[[:alpha:]]+', 1, level) is not null;

或者如果您在包含任何这些单词的表格内搜索, 你可以CONTAINS从索引的oracle中运行:

此处链接:http://docs.oracle.com/cd/B28359_01/text.111/b28303/query.htm

CTXCAT在此处搜索:http://www.oracle.com/technetwork/database/enterprise-edition/ctxcat-primer-090555.html

答案 2 :(得分:0)

SELECT * FROM tablename WHERE desc IN (
    SELECT regexp_substr('from+google+world','[^+]+', 1, level) FROM dual
    CONNECT BY regexp_substr('from+google+world', '[^+]+', 1, level)
    is not null
);