简体   繁体   English

使用 SQL Server LIKE 模式搜索“全字匹配”

[英]Search for “whole word match” with SQL Server LIKE pattern

Does anyone have a LIKE pattern that matches whole words only?有没有人有只匹配整个单词的LIKE模式?

It needs to account for spaces, punctuation, and start/end of string as word boundaries.它需要将空格、标点符号和字符串的开头/结尾作为单词边界考虑在内。

I am not using SQL Full Text Search as that is not available.我没有使用 SQL 全文搜索,因为它不可用。 I don't think it would be necessary for a simple keyword search when LIKE should be able to do the trick.LIKE应该能够做到这一点时,我认为没有必要进行简单的关键字搜索。 However if anyone has tested performance of Full Text Search against LIKE patterns, I would be interested to hear.但是,如果有人针对LIKE模式测试了全文搜索的性能,我很想听听。

Edit:编辑:

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]%' 

I want this to match "pit" but not "spit" in a sentence or as a single word.我希望它在句子中或作为单个单词匹配“pit”而不是“spit”。

Eg DealTitle might contain "a pit of despair" or "pit your wits" or "a pit" or "a pit."例如, DealTitle可能包含“a pit of despair”或“pit your wits”或“a pit”或“a pit”。 or "pit!"或“坑!” or just "pit".或者只是“坑”。

Full text indexes is the answer.全文索引就是答案。

The poor cousin alternative is可怜的表亲选择是

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

FYI unless you are using _CS collation, there is no need for a-zA-Z仅供参考,除非您使用 _CS 排序规则,否则不需要 a-zA-Z

I think the recommended patterns exclude words with do not have any character at the beginning or at the end.我认为推荐的模式排除开头或结尾没有任何字符的单词。 I would use the following additional criteria.我将使用以下附加标准。

where DealTitle like '%[^a-z]pit[^a-z]%' OR 
  DealTitle like 'pit[^a-z]%' OR 
  DealTitle like '%[^a-z]pit'

I hope it helps you guys!我希望它可以帮助你们!

you can just use below condition for whitespace delimiters:您可以使用以下条件作为空格分隔符:

(' '+YOUR_FIELD_NAME+' ') like '% doc %'

it works faster and better than other solutions.它比其他解决方案更快更好地工作。 so in your case it works fine with "a pit of despair" or "pit your wits" or "a pit" or "a pit."所以在你的情况下,它适用于“绝望的坑”或“坑你的智慧”或“坑”或“坑”。 or just "pit", but not works for "pit!".或者只是“坑”,但不适用于“坑!”。

Another simple alternative:另一个简单的选择:

WHERE DealTitle like '%[^a-z]pit[^a-z]%' OR 
      DealTitle like '[^a-z]pit[^a-z]%' OR 
      DealTitle like '%[^a-z]pit[^a-z]'

This is a good topic and I want to complement this to someone how needs to find some word in some string passing this as element of a query.这是一个很好的主题,我想补充这个给某人如何需要在某些字符串中找到一些单词并将其作为查询元素传递的内容。

SELECT 
    ST.WORD, ND.TEXT_STRING
FROM 
    [ST_TABLE] ST
LEFT JOIN 
    [ND_TABLE] ND ON ND.TEXT_STRING LIKE '%[^a-z]' + ST.WORD + '[^a-z]%'
WHERE 
    ST.WORD = 'STACK_OVERFLOW' -- OPTIONAL

With this you can list all the incidences of the ST.WORD in the ND.TEXT_STRING and you can use the WHERE clausule to filter this using some word.有了这个,你可以列出所有的发生率ST.WORDND.TEXT_STRING ,您可以使用WHERE clausule过滤此使用一些字。

Surround your string with spaces and create a test column like this:用空格包围你的字符串并创建一个像这样的测试列:

 SELECT t.DealTitle 
 FROM yourtable t
 CROSS APPLY (SELECT testDeal = ' ' + ISNULL(t.DealTitle,'') + ' ') fx1
 WHERE fx1.testDeal LIKE '%[^a-z]pit[^a-z]%'

If you can use regexp operator in your SQL query..如果您可以在 SQL 查询中使用正则regexp运算符..

For finding any combination of spaces, punctuation and start/end of string as word boundaries:查找空格、标点符号和字符串开头/结尾的任意组合作为单词边界:

where DealTitle regexp '(^|[[:punct:]]|[[:space:]])pit([[:space:]]|[[:punct:]]|$)'

Try using charindex to find the match:尝试使用charindex查找匹配项:

Select * 
from table 
where charindex( 'Whole word to be searched', columnname) > 0

You could search for the entire string in SQL:您可以在 SQL 中搜索整个字符串:

select * from YourTable where col1 like '%TheWord%'

Then you could filter the returned rows client site, adding the extra condition that it must be a whole word.然后您可以过滤返回的行客户端站点,添加它必须是整个单词的额外条件。 For example, if it matches the regex:例如,如果它匹配正则表达式:

\bTheWord\b

Another option is to use a CLR function, available in SQL Server 2005 and higher.另一种选择是使用 SQL Server 2005 及更高版本中提供的 CLR 函数。 That would allow you to search for the regex server-side.这将允许您搜索正则表达式服务器端。 This MSDN artcile has the details of how to set up a dbo.RegexMatch function.这篇MSDN 文章详细介绍了如何设置dbo.RegexMatch函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM