2017-01-03 172 views
0

在SQL中,我正在寻找逻辑来获得下面的解决方案。使用通配符字符搜索字符串的SQL

假设字符串输入是

This is the time 

案例1:当没有输入提供的查询应该返回导致"T I T T"每个单词的第一个字母。

案例2:输入是T,那么它应该显示下面的下一个字符。

实施例:"h h i"

在溶液如何可以抵任何输入?

+0

带个案陈述/解码结合做一个子串 – SomeJavaGuy

+0

感谢凯文。任何示例或指针都会很有帮助。 –

回答

0

你会为此使用REGEXP_REPLACE。首先在你的字符串前添加一个空白。因此,你总是寻找某个字母后的字符(例如't'后面的字母或空白后的字母)。

select 
    case when instr(t.text, c.chr) > 0 then 
    regexp_replace 
    (
     t.text, 
     c.chr || '([^' || c.chr || '])[^' || c.chr || ']*', -- search pattern 
    '\1 ', -- replace pattern 
     1, 0, 'i' -- from the start, all occurerences, case insensitive 
    ) 
    else 
    null 
    end if 
from (select ' ' || 'This is the time' as text from dual) t 
cross join (select coalesce(:chr, ' ') as chr from dual) c;