;WITH Num1 (n) AS (SELECT 1 UNION ALL SELECT 1),
Num2 (n) AS (SELECT 1 FROM Num1 AS X, Num1 AS Y),
Num3 (n) AS (SELECT 1 FROM Num2 AS X, Num2 AS Y),
Num4 (n) AS (SELECT 1 FROM Num3 AS X, Num3 AS Y),
Nums (n) AS (SELECT ROW_NUMBER() OVER(ORDER BY n) FROM Num4),
CleanCTE
AS
(SELECT CompanyID, CompanyName,
CAST ((SELECT CASE WHEN ASCII(SUBSTRING(CompanyName, n, 1))
BETWEEN 0x00 AND 0x1F
OR ASCII(SUBSTRING(CompanyName, n, 1)) BETWEEN 0x80 AND 0xBF
THEN ''
ELSE SUBSTRING(CompanyName, n, 1)
END + ''
FROM Company AS B
JOIN Nums
ON n <= LEN(CompanyName)
WHERE B.CompanyID = A.CompanyID
Order by Nums.N
FOR XML PATH(''), TYPE) AS VARCHAR(256)) AS CleanName
FROM Company AS A)
SELECT TOP 100 o.CompanyName, CleanName
FROM
CleanCTE cte
JOIN
Company o on o.CompanyID = cte.CompanyID
WHERE
LEN(CleanName) <> LEN(o.OperatorName)
|