【问题标题】:Split the name into Fname, Mname and Lname将名称拆分为 Fname、Mname 和 Lname
【发布时间】:2021-06-03 20:45:47
【问题描述】:

请帮忙解决这个问题

输入

Fullname
SZAFLARSKI,ANDRZEJ S
SZAFLARSKI,ANDRZEJ Santa
SZAFLARSKI rel,ANDRZEJ

输出

Firstname               Middlename       Lastname              
ANDRZEJ S SZAFLARSKI This format for length = 1
ANDRZEJ Santa null SZAFLARSKI This format for length greater than 1
ANDRZEJ null SZAFLARSKI rel It has to search for firstname after comma(,)

如果中间名长度大于 1,那么 firstname 将附加中间名,并且中间名给出 null。如果中间名长度 = 1,则使用中间名和名字拆分,直到空格之前。 Firstname 的搜索应该从逗号(,) 开始,并且第三种情况应该匹配。

谁能帮忙解释一下?

编辑

select lastN,
       case when length(midN) = 1 then midN end as midN,
       firstN || case when length(midN) > 1 then ' ' || midN end as firstN
  from ( select regexp_substr('BOULAY d,STEPHEN', '([^,]+),', 1, 1, '', 1) as lastN,
                regexp_substr('BOULAY d,STEPHEN', ',([^ ]+)', 1, 1, '', 1) as firstN,
                regexp_substr('BOULAY d,STEPHEN', ' (.+)', 1, 1, '', 1) as midN
           from dual );

在这个查询中,我已经实现了前 2 个场景,但第 3 个场景失败了。

【问题讨论】:

  • 您自己尝试过什么吗?您至少应该能够弄清楚如何获得姓氏。你知道如何在字符串中找到逗号吗?你知道如何获取字符串的子字符串吗?以下是字符串函数列表:docs.oracle.com/en/database/oracle/oracle-database/18/sqlrf/…。然后,对于“如果最后一部分是单个字符”,SQL 中有CASE WHEN。向我们展示您拥有什么以及您遇到的问题。
  • 选择lastN,当length(midN) = 1然后midN结束为midN,firstN ||长度(midN)> 1 然后 ' ' || 的情况midN end as firstN from ( select regexp_substr('BOULAY d,STEPHEN', '([^,]+),', 1, 1, '', 1) as lastN, regexp_substr('BOULAY d,STEPHEN', ', ([^ ]+)', 1, 1, '', 1) as firstN, regexp_substr('BOULAY d,STEPHEN', ' (.+)', 1, 1, '', 1) as midN from dual ;在这个查询中,我已经实现了第 2 种情况,但第 3 种情况失败了
  • 您可以edit您的问题。您评论中的 SQL 应该在您的问题中。

标签: sql oracle


【解决方案1】:

你也可以试试这个:

^  -->  Finds regex that must match at the beginning of the line
$  --> Finds regex that must match at the end of the line.
\w --> A word character, short for [a-zA-Z_0-9]
\s --> A whitespace character, short for [ \t\n\x0b\r\f]

函数length(regexp_substr(Fullname, ',\w+\s*(\w*)$', 1, 1, '', 1)) 返回中间名路径的长度(如果存在)。

with your_table (Fullname) as (
select 'SZAFLARSKI,ANDRZEJ S'      from dual union all
select 'SZAFLARSKI,ANDRZEJ Santa'  from dual union all
select 'SZAFLARSKI rel,ANDRZEJ'    from dual
)
select Fullname
, case when length(regexp_substr(Fullname, ',\w+\s*(\w*)$', 1, 1, '', 1)) > 1 
        then regexp_substr(Fullname, ',([^,]+)$', 1, 1, '', 1) 
      else regexp_substr(Fullname, ',(\w+)\s*\w*$', 1, 1, '', 1)
  end FirstName
, case when length(regexp_substr(Fullname, ',\w+\s*(\w*)$', 1, 1, '', 1)) > 1 
        then null
      else regexp_substr(Fullname, ',\w+\s*(\w*)$', 1, 1, '', 1)
    end middleName
, regexp_substr(Fullname, '^[^,]+', 1, 1)Lastname 
from your_table
;

db<>fiddle

【讨论】:

  • “你可以试试这个”并不是一个很好的答案。为什么应该他们试试这个?这个答案将受益于代码正在做什么的解释。
  • 你说得对,我应该用更多的解释来回答我的问题。我稍后再做,我现在没有太多时间。
  • 现在我为我的回答带来了更多解释。希望能有所帮助。
【解决方案2】:

使用REGEXP_SUBSTR,您可以使用^ 作为字符串的开头,使用$ 作为字符串的结尾。

使用fullname LIKE '% _' 来确定是否在末尾的空格后只有一个字符。 (当然,如果您更喜欢 REGEXP_LIKE,也可以使用它。)

select
  case when fullname like '% _'
    then regexp_substr(fullname, '([^,]+) .$', 1, 1, 'i', 1)
    else regexp_substr(fullname, '[^,]+$')
  end as first_name,
  case when fullname like '% _'
    then regexp_substr(fullname, '.$')
    else ''
  end as middle_name,
  regexp_substr(fullname, '^[^,]+') as last_name
from mytable;

演示:https://dbfiddle.uk/?rdbms=oracle_18&fiddle=c9b94e7ac222ee8c970c111ecd97e269

【讨论】:

    猜你喜欢
    • 2014-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-07
    • 2017-12-14
    • 2012-06-05
    • 2012-12-19
    • 2023-02-09
    相关资源
    最近更新 更多