简体   繁体   English

将“约翰·乔·史密斯先生”分成“先生”“约翰”“乔”“史密斯”

[英]Separate “Mr John Joe Smith” into “Mr” “John” “Joe” “Smith”

A file supplied by a Client has the following structure:客户端提供的文件具有以下结构:

FullName全名
Mr John Joe Smith约翰·乔·史密斯先生

They would like this transformed to the following:他们希望将其转换为以下内容:

Salutation称呼 FirstName MiddleName中间名字 LastName
Mr先生 John约翰 Joe Smith史密斯

I've looked into this and I've tried the following:我对此进行了调查,并尝试了以下方法:

SELECT        substring(FullName,0,charindex(' ',Fullname)) As Salutation, substring(FullName,charindex(' ',Fullname)+1,len(fullname)) As FirstName
FROM            dbo.Individuals

This produces:这会产生:

Salutation称呼 FirstName
Mr先生 John Joe Smith约翰·乔·史密斯

I'm struggling to separate it further, is this the best way of doing it or is there a better way?我正在努力将它进一步分开,这是最好的方法还是有更好的方法?

Any help would be greatly appreciated, thanks.任何帮助将不胜感激,谢谢。

As long as there's always the same four elements in the same order...只要总是有相同的四个元素以相同的顺序......

select  parsename(replace('Mr John Joe Smith',' ','.'),4) [Salutation],
        parsename(replace('Mr John Joe Smith',' ','.'),3) [FirstName],
        parsename(replace('Mr John Joe Smith',' ','.'),2) [MiddleName],
        parsename(replace('Mr John Joe Smith',' ','.'),1) [LastName]

Here's an alternative to parsename:这是 parsename 的替代方法:

SELECT *
FROM (
  SELECT FullName,Name
   ,CASE RN WHEN 1 THEN 'Salutation' WHEN 2 THEN 'FirstName' WHEN 3 THEN 'MiddleName' WHEN 4 THEN 'LastName' END AS ColName
  FROM (SELECT FullName,value AS Name,ROW_NUMBER() OVER(PARTITION BY FullName ORDER BY (SELECT NULL)) AS RN
    FROM dbo.Individuals
    CROSS APPLY STRING_SPLIT(FullName,' ') ) base ) mid
PIVOT (MAX(Name) FOR ColName IN ([Salutation],[FirstName],[MiddleName],[LastName]) ) p

You could expand the CASE statement to make it less format-sensitive.您可以扩展 CASE 语句以使其对格式不敏感。 For example:例如:

CASE
 WHEN FullName LIKE '% % % %' THEN
  CASE RN WHEN 1 THEN 'Salutation' WHEN 2 THEN 'FirstName' WHEN 3 THEN 'MiddleName' WHEN 4 THEN 'LastName' END
 WHEN FullName LIKE '% % %' THEN
  CASE RN WHEN 1 THEN 'FirstName' WHEN 2 THEN 'MiddleName' WHEN 3 THEN 'LastName' END
 WHEN FullName LIKE '% %' THEN
  CASE RN WHEN 1 THEN 'FirstName' WHEN 2 THEN 'LastName' END
END AS ColName

But this would still backfire and need additional logic if you have a mix of "Mr John Smith"s and "John Joe Smith"s, lines with more than 5 components, etc.但这仍然会适得其反并且需要额外的逻辑,如果你有“约翰史密斯先生”和“约翰乔史密斯”的混合,超过 5 个组件的行等。

STRING_SPLIT also doesn't have a guaranteed return order according to the documentation, though I've never personally seen it differ from the original string's order.根据文档,STRING_SPLIT 也没有保证返回顺序,尽管我个人从未见过它与原始字符串的顺序不同。 You coulddefine your own function if you need guaranteed ordering, or if you're on pre-compatibility-130 and don't have STRING_SPLIT.如果您需要保证订购,或者如果您使用的是 pre-compatibility-130 并且没有 STRING_SPLIT,您可以定义自己的function。

暂无
暂无

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

相关问题 SQL仅在单列中替换字符。 joe.bloggs至Joe Bloggs - SQL Replace characters in a single column only. joe.bloggs to Joe Bloggs 是否可以将“ John”和“ Jane”之类的字符串转换为数值,但保持其对“ John”和“ Jane”的可读性 - Is it possible to convert strings like 'John' and 'Jane' to numeric values but keep them readable as 'John' and 'Jane' 蜂巢加入失败mr.MapredLocalTask - hive join failed mr.MapredLocalTask Wordpress SQL命令来设置作者John Doe撰写的所有帖子 - Wordpress SQL Command to Set all posts by author John Doe to draft SQL Replace查询从名称包含(MR。,MRS。,MR / MRS…)中删除前缀 - SQL Replace query to remove prefix from name contains (MR., MRS., MR / MRS…) SQL 相同姓氏的相同国家、相同姓名、相同年龄的人数(例如 smith) - SQL Count of same country, same name, same age for people with same last name (e.g smith) 我可以用函数 mr 返回一个非分区表吗? - Can I return a non-partitioned table with function mr? MySQL-如何获得老师约翰教过的所有学生的名字? - MySQL - How can I get names of all students that were taught by teacher with name John? 如何运行我的 SQL 以获得诸如“John Doe,plumbing”之类的结果 - How can I run my SQL, to get the result such as “John Doe, plumbing” 更新查询给出了奇怪的错误“ CfPlnUt_Aemk5Mr77-AevA2” - Update query is giving weird error “CfPlnUt_Aemk5Mr77-AevA2”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM