查询从字符串中提取单词

Query to extract the word from string

我想从包含以下地址的地址字段 (Oracle 12 C) 中提取单词:-

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

我的查询应该return

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

基本上我想从大量数据行中的字符串中提取 flat no

以下回答可以帮助解决问题 -

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 )

如果您的数据类型可能相似,您可以使用 regexp_substr()

根据 Post,我在您想要的输出中发现了一些相似之处,例如:

  1. 开头为Flat no
  2. digit结束
  3. 并且可能只包含一个特殊符号/

因此,您可以基于此创建正则表达式

Flat no[A-z -]+[0-9/]+

哪个能够匹配特定的子串

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;

输出:

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

demo