从PLSQL中的3个字长的字符串中选择第一个和最后一个字

时间:2018-06-12 10:10:05

标签: string plsql substr

例如,我有这样的名字:

John Lucas Smith    
Kevin Thomas Bacon

我需要使用regexp_substr,或替换或类似的东西。

我想得到的是:

John Smith    
Kevin Bacon

谢谢!

1 个答案:

答案 0 :(得分:4)

这样的东西?

SQL> with test (col) as
  2    (select 'John Lucas Smith'   from dual union
  3     select 'Kevin Thomas Bacon' from dual union
  4     select 'Little Foot'        from dual
  5    )
  6  select regexp_substr(col, '^\w+') ||' '||
  7         regexp_substr(col, '\w+$') first_and_last
  8  from test;

FIRST_AND_LAST
-------------------------------------
John Smith
Kevin Bacon
Little Foot

SQL>