简体   繁体   English

SQL Server永久分隔名字,姓氏和中间名首字母

[英]SQL server separating first name, last name, and middle initial permanently

I have a table that has first name, last name, and middle initial, when there is one, in one column like this: 我有一个表,在这样的一列中有名字,姓氏和中间名首字母(如果有的话):

Name
Doe, John B
Smith, Bob

Some names do not have a middle initial and I just want to get rid of the ones that do. 有些名字没有中间的缩写,我只想除去那些名字的缩写。 So far I have this query: 到目前为止,我有这个查询:

SELECT   [table].[Name]
    ,LEFT([table].[Name], CHARINDEX(',', [table].[Name]) - 1) AS [Surname]
    ,REPLACE(SUBSTRING([table].[Name], CHARINDEX(',', [table].[Name]), LEN([table].[Name])), ',', '') AS [FirstName]
FROM    [table]

Which gives me this result: 这给了我这个结果:

Surname  First Name
Doe      John B
Smith    Bob

Is there something that I can add to just get rid of the middle initial when it occurs? 有什么我可以添加的,以消除出现在中间的缩写吗? Also, can I permanently change this data so it remains in two separate columns? 另外,我可以永久更改此数据,使其保留在两个单独的列中吗?

Assuming the format is strictly Last, First <Optional initial> for first without initial: 假设格式严格为Last, First <Optional initial>表示无首字母的首字母:

SUBSTRING([table].[Name], CHARINDEX(',', [table].[Name]) + 2, 
    LEN([table].[Name]) - CHARINDEX(',', [table].[Name]) 
    - CASE WHEN PATINDEX('% [A-Z]', [table].[Name]) > 0 THEN 3 ELSE 0 END)

If you want to permanently change it, create a query with what you want and overwrite the values. 如果要永久更改它,请使用所需内容创建查询并覆盖值。

Query: 查询:

WITH CTE AS (SELECT Name, LEFT(Name,CHARINDEX(',',RIGHT(Name,LEN(Name)-CHARINDEX(',',Name)))+CHARINDEX(',',Name)-1) AS [Last] FROM table)
SELECT [Last], LEFT(REPLACE(Name,Last+', ',''),CHARINDEX(' ',REPLACE(Name,Last+', ',''))-1) AS [First],
       RIGHT(REPLACE(Name,Last+', ',''),LEN(REPLACE(Name,Last+', ',''))-CHARINDEX(' ',REPLACE(Name,Last+', ',''))) AS Middle
FROM CTE

This uses a CTE for clarity and it also accounts for many special cases including names with suffixes (Jr, Sr, Esq, etc). 为了清晰起见,这使用了CTE,并且还考虑了许多特殊情况,包括带后缀的名称(Jr,Sr,Esq等)。

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

相关问题 SQL Server:将名字和姓氏分开并将中间名首字母删除为两列 - SQL Server : separate first and last name and remove middle initial into just two columns 在SQL Server中从名字中分离中间名首字母 - Split middle initial from first name in sql server 我在将SQL Server 2017中的名字与中间名列分开时遇到麻烦 - I'm having trouble separating the first name from the middle name column in SQL Server 2017 在SQL中将名称拆分为姓,名,中间名 - Split Name into Last name, First Name, Middle Name in SQL 在T-SQL中组合First,Middle Initial,Last name和Suffix(无额外空格) - Combine First, Middle Initial, Last name and Suffix in T-SQL (No extra spaces) 从 SQL Server 中的名称字符串中获取中间名和名字 - Get middle name and first name from name string in SQL Server Oracle SQL-解析名称字符串并将其转换为名字的名字和姓氏 - Oracle SQL - Parsing a name string and converting it to first initial & last name 当在几行中有完整的中间名时,查询以合并或合并第一栏中的名字,中间名缩写和姓氏? - query to merge or combine first name, middle initial and last name in single column, when Ihave full Middle name in few rows? SQL标准化/清理名字/中间/姓氏字段 - SQL to standardize/clean up First/Middle/Last name fields 将全名拆分为 First Middle Last 和 Suffix SQL - Splitting Full name into First Middle Last and Suffix SQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM