简体   繁体   English

在 SQL 中将全名拆分为名字和姓氏

[英]Split a full name into first and last name in SQL

I'm working with a table that contains a field with medical provider names, formatted as Last, First Credential.我正在处理一个表格,其中包含一个带有医疗提供者名称的字段,格式为 Last, First Credential。 I need to split the first and last name up into two separate fields, as well as cut off the credential at the end.我需要将名字和姓氏分成两个单独的字段,并在最后切断凭证。

SELECT ProviderName


ProviderName
----------------------
Smith, John MD
Doe, Jane FNP
Brown, Bob PA-C

I'm on the right track with what I've got so far...到目前为止,我正走在正确的轨道上......

SELECT
    LEFT (ProviderName, CHARINDEX(', ', ProviderName) - 1) AS ProviderLName,
    REPLACE(SUBSTRING(ProviderName, CHARINDEX(', ', ProviderName), LEN(ProviderName)), ', ', '') AS ProviderFName


ProviderLName    ProviderFName
----------------------------------
Smith            John MD
Doe              Jane FNP
Brown            Bob PA-C

...but I'm not sure what to add to the code in order to eliminate everything after the second space (aka eliminate the credentials). ...但我不确定要在代码中添加什么以消除第二个空格之后的所有内容(也就是消除凭据)。 What's my next step?我的下一步是什么?

Assuming you always want to remove everything after the last space:假设您总是想删除最后一个空格之后的所有内容:

  • Reverse the string反转字符串
  • Find the last space找到最后一个空间
  • Substring it子串
  • Reverse again.再反转。
DECLARE @Test TABLE (ProviderName VARCHAR(128));

INSERT INTO @Test (ProviderName)
VALUES
('Smith, John MD'),
('Doe, Jane FNP'),
('Brown, Bob PA-C');

SELECT ProviderName, ProviderFName, ProviderLName
    , REVERSE(SUBSTRING(REVERSE(ProviderFName), CHARINDEX(' ', REVERSE(ProviderFName))+1, LEN(ProviderFName))) ProviderFnameFinal
FROM (
    SELECT
        ProviderName 
        , LEFT (ProviderName, CHARINDEX(', ', ProviderName) - 1) AS ProviderLName
        , REPLACE(SUBSTRING(ProviderName, CHARINDEX(', ', ProviderName), LEN(ProviderName)), ', ', '') AS ProviderFName
    from @Test
) X;

Returns退货

ProviderName    ProviderFName   ProviderLName   ProviderFnameFinal
Smith, John MD  John MD         Smith           John
Doe, Jane FNP   Jane FNP        Doe             Jane
Brown, Bob PA-C Bob PA-C        Brown           Bob

One method uses string_split() and some string logic:一种方法使用string_split()和一些字符串逻辑:

select pn.*, s.*
from ProviderName pn cross apply
     (select max(case when pn.name like ',' then replace(s.value, ',', '') end) as lastname,
             max(case when pn.name like '%, ' + s.value + '%' then s.value end) as firstname
      from string_split(pn.name, ' ') s
     ) s;

Note: This assumes that there is only one comma in the string.注意:这里假设字符串中只有一个逗号。

I'm going to go ahead and answer my own question (huge thanks to @shawnt00 for your help getting me started!).我将继续回答我自己的问题(非常感谢@shawnt00帮助我开始!)。

To reiterate, if I select ProviderName from my table, I end up with results like the following.重申一下,如果我从我的表中选择ProviderName ,我最终会得到如下结果。 This table also has a generic provider option for when a patient comes in for a basic nurse visit that turns out kind of awkward in the last-name-comma-first-name format.该表还有一个通用的提供者选项,用于当患者进行基本的护士访问时,在姓氏-逗号-名字的格式中变得有点尴尬。

SELECT ProviderName

ProviderName
----------------------
Smith, John MD
Doe, Jane FNP
Brown, Bob PA-C
MA-Nurse Visit, Clinical

In order to account for the difference in formatting for that nurse visit provider, I ended up using a CASE statement in my query.为了说明该护士访问提供者的格式差异,我最终在查询中使用了CASE语句。

SELECT
    (CASE 
        WHEN ProviderName LIKE '%MA-%' THEN 
            REVERSE(SUBSTRING(REVERSE(ProviderName), 1, CHARINDEX(' ', REVERSE(ProviderName)) - 1))
        ELSE 
            LEFT(REPLACE(SUBSTRING(ProviderName, CHARINDEX(', ', ProviderName), 
            LEN(ProviderName)), ', ', ''),
            CHARINDEX(' ',REPLACE(SUBSTRING(ProviderName, CHARINDEX(', ', ProviderName), 
            LEN(ProviderName)), ', ', '')) - 1) 
    END) AS [Provider First Name],
    (CASE
        WHEN ProviderName LIKE '%MA-%' THEN 
            SUBSTRING(ProviderName, 1, CHARINDEX(' ', ProviderName) - 1)
        ELSE 
            LEFT(ProviderName, CHARINDEX(', ', ProviderName) - 1) 
    END) AS [Provider Last Name]

Which gives us...这让我们...

Provider First Name     Provider Last Name
-------------------------------------------
John                    Smith
Jane                    Doe
Bob                     Brown
Clinical                MA-Nurse

The "visit" part of the nurse provider gets cut off, but for the purposes of the data I'm pulling, that's perfectly fine, and it doesn't make the provider name any harder to understand.护士提供者的“访问”部分被切断了,但就我正在提取的数据而言,这完全没问题,并且不会使提供者的名称变得更难理解。

Hope this information is helpful to others in the future.希望这些信息在未来对其他人有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM