将泰语名称解析为姓氏
Parse Thai Name into First Last
我需要将全名列表解析为名字和姓氏。如果包含中间名,则应包含在拳名字段中。
John Smith 会是:
FirstName = John
LastName = Smith
John J. Smith 将是:
FirstName = John J.
LastName = Smith
问题是名称可能是泰语或英语字符集。我需要正确解析任何一组。我几乎什么都试过了...
DECLARE @FullName NVARCHAR(MAX) = N'กล้วยไม้ สวามิวัศดุ์'
--DECLARE @FullName NVARCHAR(MAX) = N'Mark C. Wilson'
SELECT
LEN(@FullName) AS StringLength,
LEN(@FullName) - LEN(REPLACE(@FullName,N' ', N'')),
LEN(REPLACE(@FullName,N' ', N'')),
@FullName AS FullName,
REVERSE(@FullName) AS ReverseName, -- This is obviously no Reverse of the string
CHARINDEX(N' ', REVERSE(@FullName)) AS LastSpaceLocation,
CHARINDEX(N' ', @FullName) AS FirstSpaceLocation,
LEN(@FullName) AS LenString,
STUFF(@FullName, 1, CHARINDEX(N' ', @FullName), N'') as FirstName,
RIGHT(@FullName, LEN(@FullName) - CHARINDEX(N' ', @FullName) + 1) as LastName,
LEFT(@FullName, LEN(@FullName) - CHARINDEX(N' ', REVERSE(@FullName))) AS FirstName,
STUFF(RIGHT(@FullName, CHARINDEX(N' ', REVERSE(@FullName))),1,1,N'') AS LastName,
LEN(@FullName),
REVERSE(@FullName),
REVERSE(' '),
LEN(@FullName) - CHARINDEX(reverse(' '), REVERSE(@FullName)) - LEN(' ') + 1
当使用泰语字符集时,REVERSE
根本不起作用。
我看不懂泰语(我不是很聪明),但也许这会有所帮助。
这里我们对"fix"字符串使用CROSS APPLY,然后是PasrName()
和Concat()
的小事
我应该补充一点,解析名称是一个滑坡。需要考虑
- 多字姓氏,即 De la Cruz
- 后缀即。理查德·R·卡佩莱蒂医学博士
例子
Declare @YourTable table (FullName nvarchar(100))
Insert Into @YourTable values
('John Smith')
,('John J. Smith')
,(N'กล้วยไม้ สวามิวัศดุ์')
Select A.*
,LastName = replace(parsename(S,1),'|','.')
,FirstName = replace(concat(parsename(S,4),' '+parsename(S,3),' '+parsename(S,2)),'|','.')
From @YourTable A
Cross Apply ( values (replace(replace(FullName,'.','|'),' ','.'))) B(S)
Returns
FullName LastName FirstName
John Smith Smith John
John J. Smith Smith John J.
กล้วยไม้ สวามิวัศดุ์ สวามิวัศดุ์ กล้วยไม้
编辑 2008 版本
Select A.*
,LastName = replace(parsename(S,1),'|','.')
,FirstName = replace( IsNull(parsename(S,4),'') + IsNull(' '+parsename(S,3),'') + IsNull(' '+parsename(S,2),''),'|','.')
From @YourTable A
Cross Apply ( values (replace(replace(FullName,'.','|'),' ','.'))) B(S)
我是泰国人,我知道的一件事是泰国人不使用中间名。
我需要将全名列表解析为名字和姓氏。如果包含中间名,则应包含在拳名字段中。
John Smith 会是:
FirstName = John
LastName = Smith
John J. Smith 将是:
FirstName = John J.
LastName = Smith
问题是名称可能是泰语或英语字符集。我需要正确解析任何一组。我几乎什么都试过了...
DECLARE @FullName NVARCHAR(MAX) = N'กล้วยไม้ สวามิวัศดุ์'
--DECLARE @FullName NVARCHAR(MAX) = N'Mark C. Wilson'
SELECT
LEN(@FullName) AS StringLength,
LEN(@FullName) - LEN(REPLACE(@FullName,N' ', N'')),
LEN(REPLACE(@FullName,N' ', N'')),
@FullName AS FullName,
REVERSE(@FullName) AS ReverseName, -- This is obviously no Reverse of the string
CHARINDEX(N' ', REVERSE(@FullName)) AS LastSpaceLocation,
CHARINDEX(N' ', @FullName) AS FirstSpaceLocation,
LEN(@FullName) AS LenString,
STUFF(@FullName, 1, CHARINDEX(N' ', @FullName), N'') as FirstName,
RIGHT(@FullName, LEN(@FullName) - CHARINDEX(N' ', @FullName) + 1) as LastName,
LEFT(@FullName, LEN(@FullName) - CHARINDEX(N' ', REVERSE(@FullName))) AS FirstName,
STUFF(RIGHT(@FullName, CHARINDEX(N' ', REVERSE(@FullName))),1,1,N'') AS LastName,
LEN(@FullName),
REVERSE(@FullName),
REVERSE(' '),
LEN(@FullName) - CHARINDEX(reverse(' '), REVERSE(@FullName)) - LEN(' ') + 1
当使用泰语字符集时,REVERSE
根本不起作用。
我看不懂泰语(我不是很聪明),但也许这会有所帮助。
这里我们对"fix"字符串使用CROSS APPLY,然后是PasrName()
和Concat()
我应该补充一点,解析名称是一个滑坡。需要考虑
- 多字姓氏,即 De la Cruz
- 后缀即。理查德·R·卡佩莱蒂医学博士
例子
Declare @YourTable table (FullName nvarchar(100))
Insert Into @YourTable values
('John Smith')
,('John J. Smith')
,(N'กล้วยไม้ สวามิวัศดุ์')
Select A.*
,LastName = replace(parsename(S,1),'|','.')
,FirstName = replace(concat(parsename(S,4),' '+parsename(S,3),' '+parsename(S,2)),'|','.')
From @YourTable A
Cross Apply ( values (replace(replace(FullName,'.','|'),' ','.'))) B(S)
Returns
FullName LastName FirstName
John Smith Smith John
John J. Smith Smith John J.
กล้วยไม้ สวามิวัศดุ์ สวามิวัศดุ์ กล้วยไม้
编辑 2008 版本
Select A.*
,LastName = replace(parsename(S,1),'|','.')
,FirstName = replace( IsNull(parsename(S,4),'') + IsNull(' '+parsename(S,3),'') + IsNull(' '+parsename(S,2),''),'|','.')
From @YourTable A
Cross Apply ( values (replace(replace(FullName,'.','|'),' ','.'))) B(S)
我是泰国人,我知道的一件事是泰国人不使用中间名。