转义括号 [ 在 CONTAINS() 子句中?

2021-09-10 00:00:00 tsql sql-server

如何在全文 SQL Server contains() 查询中转义括号?我已经尝试了以下所有方法,没有有效:

How can I escape a bracket in a full-text SQL Server contains() query? I've tried all the following, none of which work:

CONTAINS(crev.RawText, 'arg[0]')
CONTAINS(crev.RawText, 'arg[[0]]')
CONTAINS(crev.RawText, 'arg\[0\]')

使用双引号确实有效,但它强制整个搜索成为一个词组,这是多词查询的一个亮点.

Using double quotes does work, but it forces the entire search to be a phrase, which is a showstopper for multiple word queries.

CONTAINS(crev.RawText, '"arg[0]"')

我真正想做的就是摆脱括号,但我似乎无法做到..

All I really want to do is escape the bracket, but I can't seem to do that..

推荐答案

您不必转义 [,因为它在全文搜索中没有特殊含义.但是,如果您确实需要搜索完全匹配项,则可以使用 "" 标记.

You don't have to escape the [ as it has no special meaning in Full Text Search. If you do need to search for an exact match though, you can use "" marks.

此外,您可以在单引号内使用多个":

Further, you can use multiple "" inside the single quotes:

CONTAINS('"word1" or "word2" or "word3"')

这也有效:

CONTAINS('"word1" and "word2" and "word3"')

双引号内的任何内容都被视为精确文本.因此,如果我要搜索 AdventureWorks 中 Production.ProductDescription 表的 Description 字段,我可以使用

Anything put inside the double quotes is treated as exact text. Thus if I were to do a search of the Description field of the Production.ProductDescription table in AdventureWorks, I could use

CONTAINS('shifting and "on or off-road"') 

并且它会找到与on or off-road"这个词组匹配的词 shift.

and it would find matches for the word shifting that also had the phrase "on or off-road".

唯一的特殊符号是 ~,它可以用来代替 NEAR 命令.

The only special symbol is the ~, it can be used in place of the NEAR command.

CONTAINS('shifting ~ smooth')

CONTAINS('shifting NEAR smooth')

and 将找到单词 shift 和 smooth 彼此靠近的匹配项.

and will find matches where the words shifting and smooth are near each other.

相关文章