Home » SQL & PL/SQL » SQL & PL/SQL » Getting a word which contains some strings (Oracle)
Getting a word which contains some strings [message #629403] Mon, 08 December 2014 11:09 Go to next message
jaggy
Messages: 15
Registered: November 2014
Location: India
Junior Member
I have a sentence which consists of different words and I have some letter of a word from the sentence. Can some one please help me how I can get that word which contains those letters. I have tried with substr,instr but no result.

Eg,

Sentence:
I know English very well.

I have the some letters like "gli", I need some oracle query which can give the total word English.
Re: Getting a word which contains some strings [message #629404 is a reply to message #629403] Mon, 08 December 2014 11:10 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
Please read and follow the forum guidelines, to enable us to help you:

http://www.orafaq.com/forum/t/88153/0/ and read http://www.orafaq.com/forum/t/174502/


what does problem above have to do with Oracle DB?
Re: Getting a word which contains some strings [message #629405 is a reply to message #629404] Mon, 08 December 2014 11:24 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
Convert each word into different rows using regexp(should be simple, just google), then filter the rows using like '%letters_to_search%'
Re: Getting a word which contains some strings [message #629501 is a reply to message #629403] Wed, 10 December 2014 00:38 Go to previous messageGo to next message
Shreya Aggarwal
Messages: 4
Registered: December 2014
Junior Member
Hi,

You can use regular expressions for any such requirement.

Here in this particular case,regexp_substr function can be used.

Here is the sample code:

SELECT Regexp_substr('I know English very well.', '[[:alpha:]]+(gli)[[:alpha:]]+' 
       , 1, 1, 
              'i') "REGEXP_INSTR" 
FROM   dual;


Please refer to the following links for more information on using REGEXP_SUBSTR function and regular expressions in Oracle:

https://docs.oracle.com/cd/B12037_01/server.101/b10759/functions116.htm

https://docs.oracle.com/cd/B19306_01/appdev.102/b14251/adfns_regexp.htm
Re: Getting a word which contains some strings [message #629596 is a reply to message #629501] Thu, 11 December 2014 15:18 Go to previous messageGo to next message
Bill B
Messages: 1971
Registered: December 2004
Senior Member
Your Regexp_substr select will only return the first word, not all of them.
Re: Getting a word which contains some strings [message #629609 is a reply to message #629596] Fri, 12 December 2014 03:29 Go to previous message
Shreya Aggarwal
Messages: 4
Registered: December 2014
Junior Member
Hi Bill,

In case we want all the matching words in a sentence then we can use following query:

  WITH string_tab 
     AS (SELECT 1                                   id, 
                'I watched English Winglish.' val 
         FROM   dual 
         UNION 
         SELECT 2                           ID, 
                'I know English very well.' val 
         FROM   dual), 
     --seaching for the given string in the sentence 
     searched_string 
     AS (SELECT id, 
                LEVEL 
                   occurance, 
                val, 
                Regexp_substr(val, '[[:alpha:]]*(gli)[[:alpha:]]*', 1, LEVEL, 
                'i') 
                VALUE 
         FROM   string_tab 
         CONNECT BY LEVEL < Regexp_count(val, '(gli)') + 1 
                    AND PRIOR id = id 
                              AND PRIOR dbms_random.value IS NOT NULL) 
--grouping the searched words 
SELECT id, 
       val, 
       Listagg (value, ',') 
         within GROUP ( ORDER BY occurance ) Words
FROM   searched_string 
GROUP  BY id, 
          val; 



This code will return the comma separated list of words containing that string.
Previous Topic: RAW TO HEX
Next Topic: Sorting of Data
Goto Forum:
  


Current Time: Fri Apr 26 06:01:44 CDT 2024