Home Forums SQL Server 2005 T-SQL (SS2K5) Searching removing special characters using LIKE pattern or Patindex RE: Searching removing special characters using LIKE pattern or Patindex

  • I now tried to build a long string without ranges, and that seemed to work.

    DECLARE @findstr NVARCHAR(50)

    SET @findstr = '%['+CHAR(1)+CHAR(2)+CHAR(3)+CHAR(4)+CHAR(5)+CHAR(6)+CHAR(7)+CHAR(8)+CHAR(11)+CHAR(12)+CHAR(14)+CHAR(15)+CHAR(16)+CHAR(17)+CHAR(18)+CHAR(19)+CHAR(20)+CHAR(21)+CHAR(22)+CHAR(23)+CHAR(24)+CHAR(25)+CHAR(26)+CHAR(27)+CHAR(28)+CHAR(29)+CHAR(30)+CHAR(31)+']%'

    SELECT COUNT(*) FROM myTable WHERE patindex(@findstr, col1) > 0

    -- Result: 5

    Also, for my current purposes, building up ranges not including char 9, 10 and 13 results in the same 5 records:

    DECLARE @findstr NVARCHAR(50)

    SET @findstr = '%['+CHAR(1)+'-'+CHAR(8)+CHAR(11)+CHAR(12)+CHAR(14)+'-'+CHAR(31)+']%'

    SELECT COUNT(*) FROM myTable WHERE patindex(@findstr, col1) > 0

    So... I'm still not able to figure out why the SQL in my initial question does not work... probably something with Char(13)... even though it was not until I included CHAR(14) in the range that the result became 0 rows. Does anyone have an explanation for that?