I need to parse a list of FullNames into First and Last Name. If a middle name is included, it should be included in the fist name field.
John Smith would be:
FirstName = John
LastName = Smith
John J. Smith would be:
FirstName = John J.
LastName = Smith
The issue is the names might be either Thai or English character set. I need to properly parse either set. I have tried just about everything...
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
The REVERSE
simply does not work when the Thai character set is used.