The characters in brackets [ ] have to be unique, and you can escape wildcards with a slash; this here works:
SELECT PATINDEX(N'%[/^][/^]%', N'^a^bc^^') as [^]
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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.
The characters in brackets [ ] have to be unique, and you can escape wildcards with a slash; this here works:
SELECT PATINDEX(N'%[/^][/^]%', N'^a^bc^^') as [^]
My testing suggests that for the cirumflex, you get the expected result as long as you have another character in front. Olaf was half-way there, but suggested that it was escaping. It is not. So assuming that you have will have a bigger exclusion set that just ^
, there should be no difficulty.
The right bracket is more problematic, though.