是否包含等同于 SQL 服务器中的喜欢

Is Contains Equivalent To Like In SQL Server

当我 运行 这个查询:

Select * from Table1 Where Column1 Like 'aaa%' --3 Result
Select * from Table1 Where Column1 Like 'a%' --3 Result
Select * from Table1 Where Column1 Like 'A%' --3 Result

但是当我 运行

Select * from Table1 Where Contains(Column1 ,'aaa') --3 Result
Select * from Table1 Where Contains(Column1 ,'a') --0 Result
Select * from Table1 Where Contains(Column1 ,'A') --0 Result

CONTAINS可以搜索:As Per MSDN

  1. 一个词或短语。
  2. 单词或短语的前缀。
  3. 一个词靠近另一个词。

这是否意味着Contains不能搜索字母?

如果是,那怎么办?

编辑2:

declare @param as varchar(20)='a'
select * from table1 where Contains(column1,@param)

这是有效的,

declare @param as varchar(20)='"a*"'
select * from table1  where Contains(column1,@param)

但是,这不是

declare @param as varchar(20)='a'
select * from table1  where Contains(column1,@param+'*')

并且,

select * from table1  where Contains(column1,'"'+@param+'*"')

您需要使用星号来执行前缀搜索:

WHERE CONTAINS(Column1 , ' "a*" ');
WHERE CONTAINS(Column1 , ' "A*" ');

除此之外,CONTAINS 受停用词过滤器的约束。阅读那些 here

A stopword can be a word with meaning in a specific language, or it can be a token that does not have linguistic meaning. For example, in the English language, words such as "a," "and," "is," and "the" are left out of the full-text index since they are known to be useless to a search.

要将输入作为参数传递,只需附加星号:

declare @SearchThis varchar(10) = 'A'; 
set @SearchThis = quotename(@SearchThis + '*', '"');
select @SearchThis;

设置 SearchThis 后,您可以在以下位置使用:

WHERE CONTAINS(Column1, @SearchThis)

CONTAINSLIKE厉害多了。来自 MSDN...

Comparison of LIKE to Full-Text Search

In contrast to full-text search, the LIKE Transact-SQL predicate works on character patterns only. Also, you cannot use the LIKE predicate to query formatted binary data. Furthermore, a LIKE query against a large amount of unstructured text data is much slower than an equivalent full-text query against the same data. A LIKE query against millions of rows of text data can take minutes to return; whereas a full-text query can take only seconds or less against the same data, depending on the number of rows that are returned and their size. Another consideration is that LIKE performs only a simple pattern scan of an entire table. A full-text query, in contrast, is language aware, applying specific transformations at index and query time, such as filtering stopwords and making thesaurus and inflectional expansions. These transformations help full-text queries improve their recall and the final ranking of their results.

对于您的具体问题,您需要其他答案指示的前缀搜索。但是请转到我链接的 MSDN 页面。它会帮助你。