] 符号的 mssql patindex

mssql patindex for ] symbol

如何在字符class内指定]符号(MS SQL SERVER PATINDEX函数)?

'%["[]%' - for starting bracket - it works
'%["]]%' - for ending - it does not

似乎无法在 PATINDEX 中正确转义右括号 (])。 ]单独可以written verbatim,但不能包含在字符集中

但是,根据 this DBA.SE question,有一些解决方法(完整示例请参阅链接文章):

  1. Specify character range that contains ]. (note that this will match unwanted characters)
PATINDEX('%[[-^{}:,]%' COLLATE Latin1_General_BIN2, MyJSONString)
  1. Apply REPLACE before match.
PATINDEX('%[[' + CHAR(174) + '@]%', REPLACE(@test,']',CHAR(174)))
  1. Use PATINDEX twice: one for ], and the other for the rest of characters.
(NULLIF(PATINDEX('%[[{}:,]%', d.ResponseJSON), 0), NULLIF(PATINDEX('%]%', d.ResponseJSON), 0)))

在测试了不同的选项后,这似乎按预期工作。试一试。

PATINDEX('%[^]]%', 'test[test]') +1

添加“+1”是因为在我执行的每个测试中它总是在结束括号“]”之前停止一个字符,这确保捕获开仓和平仓。

试一试,告诉我你的想法。