2017-01-03 175 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;