本文介绍了使用 SQL Server LIKE 模式搜索“全字匹配"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人有只匹配整个单词的 LIKE 模式?

Does anyone have a LIKE pattern that matches whole words only?

它需要将空格、标点符号和字符串的开始/结束作为单词边界.

It needs to account for spaces, punctuation, and start/end of string as word boundaries.

我没有使用 SQL 全文搜索,因为它不可用.当 LIKE 应该能够做到这一点时,我认为没有必要进行简单的关键字搜索.但是,如果有人针对 LIKE 模式测试了全文搜索的性能,我很想听听.

I am not using SQL Full Text Search as that is not available. I don't think it would be necessary for a simple keyword search when LIKE should be able to do the trick. However if anyone has tested performance of Full Text Search against LIKE patterns, I would be interested to hear.

我已经到了这个阶段,但它不匹配字符串的开头/结尾作为单词边界.

I got it to this stage, but it does not match start/end of string as a word boundary.

where DealTitle like '%[^a-zA-Z]pit[^a-zA-Z]%'

我希望它与句子或单个单词中的pit"匹配,而不是spit".

I want this to match "pit" but not "spit" in a sentence or as a single word.

例如DealTitle 可能包含a pit of despair"或pit your wits"或a pit"或a pit".或坑!"或者只是坑".

E.g. DealTitle might contain "a pit of despair" or "pit your wits" or "a pit" or "a pit." or "pit!" or just "pit".

推荐答案

全文索引就是答案.

可怜的表亲选择是

'.' + column + '.' LIKE '%[^a-z]pit[^a-z]%'

仅供参考,除非您使用 _CS 排序规则,否则不需要 a-zA-Z

FYI unless you are using _CS collation, there is no need for a-zA-Z

这篇关于使用 SQL Server LIKE 模式搜索“全字匹配"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 13:44