[英]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.