Since we are talking about free text, it is unlikely that the data is as regular throughout as you show in your example. But here is a query which works in that ideal case. It also makes the assumption that there are no periods in the data, because I take the sneaky way of using parsename. Parsename() is a built-in function that is intended to crack object names in SQL Server. In order to that, I replace the spaces with periods.
Anyway, here is the query:
CREATE TABLE Person (PersonID int NOT NULL,
Name varchar(40) NOT NULL)
go
INSERT Person (PersonID, Name)
VALUES(1, 'jon acer sham'),
(2, 'rat kasm alo'),
(3, 'haik dosk kam'),
(4, 'sam jon acer'),
(5, 'dosk kam rad'),
(6, 'assed rat kasm')
go
WITH periods AS (
SELECT PersonID, replace(Name, ' ', '.') AS dottedname
FROM Person
), matches AS (
SELECT CASE n.n WHEN 1 THEN a.PersonID ELSE b.PersonID END AS PersonID,
CASE n.n WHEN 1 THEN a.dottedname ELSE b.dottedname END AS dottedname,
a.PersonID AS SortID, n.n
FROM periods a
JOIN periods b ON parsename(a.dottedname, 3) = parsename(b.dottedname, 2)
AND parsename(a.dottedname, 2) = parsename(b.dottedname, 1)
CROSS APPLY (VALUES(1), (2)) AS n(n)
)
SELECT PersonID, replace(dottedname, '.', ' ') AS Name
FROM matches
ORDER BY SortID, n
go
DROP TABLE Person
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se
No comments:
Post a Comment