I was writing a functionality that is the inverse of padding, e.g. from 00123 get 123, and I wanted a function that removes any given character. I use PATINDEX with [^] (Wildcard - Character(s) Not to Match), and I tested the special characters, and for my both positive and negative surprise, I could do nothing to escape these. Namely: as a positive surprise, characters _, %, and [ doesn't even need escaping, and as a negative surprise, characters ^ and ] can not be searched without escaping, and I could not escape them the way it is described at the LIKE operator, neither on a 2012 nor on a 2019 version.
Some examples without escaping:
SELECT
PATINDEX(N'%[^_]%', N'_a_bc__') as [_]
,PATINDEX(N'%[^%]%', N'%a%bc%%') as [%]
,PATINDEX(N'%[^^]%', N'^a^bc^^') as [^]
,PATINDEX(N'%[^[]%', N'[a[bc[[') as [[]
,PATINDEX(N'%[^]]%', N']a]bc]]') as []]]
SELECT
PATINDEX(N'%[^_]%', N'a_bc__') as [_]
,PATINDEX(N'%[^%]%', N'a%bc%%') as [%]
,PATINDEX(N'%[^^]%', N'a^bc^^') as [^]
,PATINDEX(N'%[^[]%', N'a[bc[[') as [[]
,PATINDEX(N'%[^]]%', N'a]bc]]') as []]]
SELECT
PATINDEX(N'%[^_]%', N'__a_bc__') as [_]
,PATINDEX(N'%[^%]%', N'%%a%bc%%') as [%]
,PATINDEX(N'%[^^]%', N'^^a^bc^^') as [^]
,PATINDEX(N'%[^[]%', N'[[a[bc[[') as [[]
,PATINDEX(N'%[^]]%', N']]a]bc]]') as []]]
Can ^ and ] be escaped in any way?
EDIT:
I opened an idea for this as a bug.