我的一个SQL查询在数据中返回不可打印的字符,因为我在报告中收到错误。请告诉我如何检查字符串是否在T-SQL中具有不可打印的字符,以便我可以找到这些行并修复数据?提前谢谢。
答案 0 :(得分:2)
如果您不知道哪个不可打印的字符导致了问题,但您已确定记录:
现在,您可以使用更新执行更新来清理数据,或者只是在查询中执行清理,因为现在您知道了什么'导致问题的原因。
答案 1 :(得分:1)
Found这个:
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),
UpdateCTE
AS
(SELECT keycol, DesNM,
(SELECT CASE WHEN ASCII(SUBSTRING(DesNM, n, 1))
BETWEEN 0x00 AND 0x1F
THEN ''
ELSE SUBSTRING(DesNM, n, 1)
END + ''
FROM I2DE AS B
JOIN Nums
ON n <= LEN(DesNM)
WHERE B.keycol = A.keycol
FOR XML PATH('')) AS DesNM_clean
FROM I2DE AS A)
UPDATE UpdateCTE
SET DesNM = DesNM_clean;
它有几个部分,一个是生成一个带数字的表 在运行中(使用NumX CTE),然后将列切成单独的 字符,检查每个字符是否在范围内然后跳过它, 最后使用FOR XML PATH连接回单个值。
答案 2 :(得分:0)
我发现有些列的字符CHAR(0)导致了这个问题。我将它替换为null并且有效。