使用通配符字符

时间:2017-01-03 12:40:32

标签: sql oracle pattern-matching

在SQL中,我正在寻找以下解决方案的逻辑。

假设字符串输入为

This is the time

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

案例2 :输入为T,然后应显示下面的下一个字符。

示例:"h h i"

有关解决方案如何到达的任何意见?

1 个答案:

答案 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;