我希望从varchar2
列的中间提取一些文本。
以下是一些示例:
TEST DATA - SCOTLAND 1A
TEST DATA - ENGLAND 6A
TEST DATA - WALES 3A
TEST DATA - IRELAND 2A
我要返回的数据将是:-
SCOTLAND
ENGLAND
WALES
IRELAND
非常感谢
李
答案 0 :(得分:0)
此查询似乎有效:
SELECT
input,
REGEXP_REPLACE(input, '.*- (\D+).*', '\1') AS country
FROM yourTable;
在这里,我们将REGEXP_REPLACE
与以下模式一起使用:
.*- (\D+).*
这将捕获在破折号和国家名称后面的空格之间出现的所有非数字字符。然后,我们只用捕获的国家名称代替。
答案 1 :(得分:0)
这可能是一种方法,假设您需要获取(唯一的)“-”和第一位数字之间的字符串部分。
with testTable(string) as (
select 'TEST DATA - SCOTLAND 1A' from dual union all
select 'TEST DATA - ENGLAND 6A' from dual union all
select 'TEST DATA - WALES 3A' from dual union all
select 'TEST DATA - IRELAND 2A' from dual union all
select 'TEST DATA - NORTHERN IRELAND 2A' from dual
)
select string,
regexp_substr(string, '\- ([^0-9]*)[0-9]', 1, 1, 'i', 1) result
from testTable
STRING RESULT
------------------------------- -------------------------------
TEST DATA - SCOTLAND 1A SCOTLAND
TEST DATA - ENGLAND 6A ENGLAND
TEST DATA - WALES 3A WALES
TEST DATA - IRELAND 2A IRELAND
TEST DATA - NORTHERN IRELAND 2A NORTHERN IRELAND
5 rows selected.
答案 2 :(得分:0)
感谢Aleksej的CTE。
又好又旧的SUBSTR + INSTR
呢?
SQL> with testTable(string) as (
2 select 'TEST DATA - SCOTLAND 1A' from dual union all
3 select 'TEST DATA - ENGLAND 6A' from dual union all
4 select 'TEST DATA - WALES 3A' from dual union all
5 select 'TEST DATA - IRELAND 2A' from dual union all
6 select 'TEST DATA - NORTHERN IRELAND 2A' from dual
7 )
8 select
9 string,
10 trim(substr(string,
11 instr(string, '-') + 1,
12 instr(string, ' ', -1) - instr(string, '-')
13 )) result
14 from testtable;
STRING RESULT
------------------------------- -------------------------------
TEST DATA - SCOTLAND 1A SCOTLAND
TEST DATA - ENGLAND 6A ENGLAND
TEST DATA - WALES 3A WALES
TEST DATA - IRELAND 2A IRELAND
TEST DATA - NORTHERN IRELAND 2A NORTHERN IRELAND
SQL>
答案 3 :(得分:0)
一个选择是首先将regexp_replace
用作模式,查找数字,以将国名作为第三个单词,而将regexp_substr
应用于仅提取这些名字。
with cte as
(
select 'TEST DATA - SCOTLAND 1A' as str from dual union all
select 'TEST DATA - ENGLAND 6A' from dual union all
select 'TEST DATA - WALES 3A' from dual union all
select 'TEST DATA - IRELAND 2A' from dual
)
select regexp_substr(
regexp_replace(str, '-(.*)\d.*', '\1' )
, '[^ ]+', 1 , 3 )
as "Country"
from cte;
Country
--------
SCOTLAND
ENGLAND
WALES
IRELAND