此代码基本上将基于一个字符串中的位置的字符转换为另一个字符串中相同位置的字符,并针对表中的所有行运行。
当我运行这个(简化版):
DECLARE @R char(40)
DECLARE @U char(40)
SET @R=' abcdefghijklmnopqrstuvwxyz!@#$%^&*()_+'+char(181)
SET @U=REVERSE(@R)
DECLARE @TestTable TABLE (RowID int identity(1,1) primary key, Unreadable varchar(500))
INSERT INTO @TestTable VALUES ('+µt$zw!*µsu+yt!+s$xy')
INSERT INTO @TestTable VALUES ('%*!!xµpxu!(')
INSERT INTO @TestTable VALUES ('pxpµnxrµu+yµs%$t')
;WITH CodeValues AS
(
SELECT
Number,SUBSTRING(@R,Number,1) AS R,ASCII(SUBSTRING(@U,Number,1)) AS UA
FROM Numbers
WHERE Number<=LEN(@R)
)
SELECT
t.RowID
,(SELECT
''+c.R
FROM Numbers n
INNER JOIN CodeValues c ON ASCII(SUBSTRING(t.Unreadable,n.Number,1))=c.UA
WHERE n.Number<=LEN(t.Unreadable)
FOR XML PATH('')
) AS readable
FROM @TestTable t
我得到以下信息:
RowID readable
----------- ---------------------------------------
1 a simple translation
2 hello world
3 wow you ran this
但需要:
RowID readable
----------- ---------------------------------------
1 a simple translation
2 hello world
3 wow you ran this
除了 ,还有什么方法REPLACE()
可以让空格正确显示吗?在我的实际代码中,这也发生在换行符上。
这可以以更好的方式重写吗?我基本上只是使用 将FOR XML PATH('')
各个行值连接在一起。