TSQL patindex 中的 CHAR(13) 或 CHAR(14) 有什么问题?一旦我在模式中包含 CHAR(14),我就找不到任何记录。在寻找答案时,我刚刚从 2009 年(这里:http ://www.sqlservercentral.com/Forums/Topic795063-338-1.aspx)找到了我自己的问题(未回答)。
这是另一个简单的测试,以说明我的意思:
/* PATINDEX TEST */
DECLARE @msg NVARCHAR(255)
SET @msg = 'ABC' + NCHAR(13) + NCHAR(9) + 'DEF'
DECLARE @unwanted NVARCHAR(50)
-- unwanted chars in a "chopped up" string
SET @unwanted = N'%[' + NCHAR(1) + '-' + NCHAR(13) + NCHAR(14) + '-' + NCHAR(31) + ']%'
SELECT patindex(@unwanted, @msg)
-- 结果:4
-- NOW LET THE unwanted string includ the whole range from 1 to 31
SET @unwanted = '%['+NCHAR(1)+'-'+NCHAR(31)+']%' -- -- As soon as Char(14) is included, we get no match with patindex!
SELECT patindex(@unwanted, @msg)
-- 结果:0