简体   繁体   中英

Query to extract the word from string

I want to extract words from a Address field (Oracle 12 C) containing the below address:-

str1: Abc.. Flat no - 8956, 8th road, Scramendo 4th street,Portland.
str2: Abcd.. Flat no Ad- 3434/89/69 Scramendo 4th street,Portland.

My query should return

  1. Flat no - 8956. (From str1 )
  2. Flat no Ad- 3434/89/69 (From str2)

Basically I want to extract the flat no from the string in huge set of data rows

You can use regexp_substr() if you data may similar kind.

As Per the Post I found some Similarity in your desired output like:

  1. It start with Flat no
  2. End with digit
  3. and may Contain only one special symbol /

So based on that you may create Regular expression

Flat no[Az -]+[0-9/]+

Which able to match specific substring

SELECT 
regexp_substr('Abc.. Flat no - 8956, 8th road ,
 Scramendo 4th street,Portland','Flat no[A-z -]+[0-9/]+') AS output FROM dual;

SELECT 
regexp_substr('Abcd.. Flat no Ad- 3434/89/69 Scramendo 4th street,Portland',
'Flat no[A-z -]+[0-9/]+') AS output FROM dual;

output:

Flat no - 8956
Flat no Ad- 3434/89/69

demo

Below answer can help in resolving the issue -

select 
substr(x, instr(x, 'Flat no',1)) from (
select 
--regexp_substr('Abcd.. Flat no- 3434/89/69 Scramendo 4th street,Portland.', '[[Flat no- ][0-9]*+'),
--substr(
substr(
    'Abcd.. Flat no- 3434/89/69 Scramendo 4th street,Portland.', 1,
REGEXP_INSTR( 'Abcd.. Flat no- 3434/89/69 Scramendo 4th street,Portland.',
   '[a-zA-Z]'
 ,instr('Abcd.. Flat no- 3434/89/69 Scramendo 4th street,Portland.', '-'),1) - 1
 ) x --, instr('Abcd.. Flat no- 3434/89/69 Scramendo 4th street,Portland.'),1 )
from dual )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM