문제

누군가가 전체 이름 필드를 구문 분석하는 데 도움이 될 수 있는지 궁금했습니다. 나는 그것을 마지막 이름, FirstName, Middle Initial, 접미사로 분리하고 싶습니다.

다음은 이름에 대한 입력과 내가 구문 분석하기를 원하는 방법입니다.

                           Parsed Stuff Begins Here-------------------------------------
    name                  | lastname  | firstname        |  middle initial   | suffix |
----------------------------------------------------------------------------------------
PUBLIC, JOHN              | PUBLIC    | JOHN             |  NULL             | NULL
PUBLIC, CHUN CH KIM       | PUBLIC    | CHUN CH KIM      |  NULL             | NULL
PUBLIC, MARY L            | PUBLIC    | MARY             |  L                | NULL
PUBLIC, FRED J JR         | PUBLIC    | FRED             |  J                | JR
PUBLIC, SUE ELLEN J SR    | PUBLIC    | SUE ELLEN        |  J                | SR

입력 할 수있는 모든 접미사 값 목록이 있습니다.

JR, SR, I,II,III,IV,V,VI

나는 마지막 이름과 나머지 이름을 나누는 지점에 도달했지만 나머지는 어떻게하는지 알 수 없습니다. Oracle 10G를 사용하고 있습니다.

이것은 숙제 질문이 아닙니다. 내가 직장에서 일하고있는 실제 문제입니다.

내가 현재 가지고있는 것은 다음과 같습니다.

 select id,
        name,
        substr(name,1, instr(name,',')-1) as lname,
        substr(name,(instr(name,',')+1),length(name)) as rest_of_the_name
 from    my_table
 where status='A';
도움이 되었습니까?

해결책

이미 부분적으로 해결했습니다. 쿼리를 하위 쿼리로 사용하고 문제를 조금씩 분해 할 수 있습니다.

select id, name, lname,
       case
       when substr(x, -2, 1) = ' '
       then substr(x, length(x) - 2)
       else x
       end as first_name, -- e.g. "SUE ELLEN"
       case
       when substr(x, -2, 1) = ' ' 
       then substr(x, -1)
       else null
       end as middle_initial, -- e.g. "J"
       suffix -- e.g. "SR"
from (
select id, name, lname, suffix,
       case when suffix is not null then
       substr(rest_of_the_name, 1, length(rest_of_the_name)-length(suffix)-1)
       else rest_of_the_name end
       as x -- e.g. "SUE ELLEN J"
from (
select id, name, lname, rest_of_the_name,
       case
       when substr(rest_of_the_name,-2)
            in (' I',' V')
       then substr(rest_of_the_name,-1)
       when substr(rest_of_the_name,-3)
            in (' JR',' SR',' II',' IV',' VI')
       then substr(rest_of_the_name,-2)
       when substr(rest_of_the_name,-4)
            in (' III')
       then substr(rest_of_the_name,-3)
       else null
       end as suffix -- e.g. "SR"
from (
select id,
       name, --e.g. "PUBLIC, SUE ELLEN J SR"
       trim(substr(name,1, instr(name,',')-1)) as lname, -- e.g. "PUBLIC"
       trim(substr(name,(instr(name,',')+1),length(name)))
          as rest_of_the_name -- e.g. "SUE ELLEN J SR"
from    my_table
where status='A'
)));

다른 팁

이것은 항상 그것을 깨뜨리는 데이터가있는 문제입니다.

이니셜 2 개가 있으면 어떻게해야합니까? J Edgar Hoover와 같이 이니셜이 먼저 있으면 어떻게해야합니까?

당신은 "하나가 들어갈 수 있다는 가치를 언급합니다. 이미 분리 된 값을 캡처하기 위해 값을 입력하는 방식을 변경할 수 있습니까?

다음은 이름과 성을 검색하여 이름과 같은 방식으로 MI를 검색하여 'REST_OF_THE_NAME'에서 성으로 MI를 제거하는 데 기반을 둔 정답이 있습니다.

SELECT
substr('John Q. Public',1, instr('John Q. Public',' ')-1) as FirstName,
substr('John Q. Public',(instr('John Q. Public',' ')+1),length('John Q. Public')) as rest_of_the_name,
substr(substr('John Q. Public',(instr('John Q. Public',' ')+1),length('John Q. Public')),1, instr(substr('John Q. Public',(instr('John Q. Public',' ')+1),length('John Q. Public')),' ')-1) as MI,
replace(substr('John Q. Public',(instr('John Q. Public',' ')+1),length('John Q. Public')), substr(substr('John Q. Public',(instr('John Q. Public',' ')+1),length('John Q. Public')),1, instr(substr('John Q. Public',(instr('John Q. Public',' ')+1),length('John Q. Public')),' ')-1)) as LastName
FROM DUAL;

substr (name, instr (name, '') +1)를 마지막 이름으로, substr (name, 1, instr (name, ''-1))로 선택하십시오.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top