在表格(football_team)中,列(名称)中的值看起来像Andrew Luck , QB
。我想使用PL / SQL函数将此列拆分为3列first_name,Last_name,position。
我试过这个
select regexp_substr(names,'[^ ,"]+',1,1) as first_name,
regexp_substr(names,'[^ ,"]+',1,2) as last_name,
regexp_substr(names,'[^ ,"]+',1,3) as position from football_team;
不起作用
我是否可以仅使用SUBSTR
和INSTR
函数来制作它。
请帮帮我。提前谢谢。
答案 0 :(得分:0)
是的,你也可以使用字符串函数,但IMO regexp在这里要简单得多。只要你能阅读正则表达式,YMMV。
问题在于正则表达式。试试这个:
with names(name) as (
select 'Andrew Luck , QB' from dual
union all
select 'John Doe , WB' from dual
)
select
regexp_substr(name, '^([[:alpha:]]+)', 1, 1, '', 1) as firstname
,regexp_substr(name, '^[[:alpha:]]+[[:space:]]+([[:alpha:]]+)', 1, 1, '', 1) as lasttname
,regexp_substr(name, '([[:alpha:]]+)$', 1, 1, '', 1) as position
from names
;
返回:
FIRSTNAME LASTNAME POSITION
--------- -------- --------
Andrew Luck QB
John Doe WB
匹配正则表达式的名字解释为:
^ -- start of the string
( -- start of subexpression group that is referenced by regexp_substr parameter #6 (subexpr)
[ -- start of matching character list
[:alpha:] -- character class expression: alphabet.
] -- end of matching character list
+ -- matches one or more occurrences of the preceding subexpression
) -- end of subexpression group
其他两个正则表达式的解释留作OP的练习。