SQL检查文本是否包含单词

问题描述 投票:-3回答:4

我有一个文字,

'Me and you against the world' // false
'Can i have an email address'  // true
'This is an'  // true
'an' //true

我想检查单词an是否在我的String中。

如何检查文本中是否包含SQL中的特定单词?我无法添加全文目录。否则我会的

SELECT * FROM TABLE WHERE CONTAINS(Text, 'an')
sql string sql-server-2005
4个回答
8
投票

这是一种方法。

DECLARE @table_name table (
   column_name varchar(50)
);

INSERT INTO @table_name (column_name)
  VALUES ('Me and you against the world')
       , ('Can i have an email address')
       , ('This is an')
;

SELECT column_name
FROM   @table_name
WHERE  ' ' + column_name + ' ' LIKE '% an %'
;

2
投票

有一些方法可以做到这一点,似乎你想找到一个单词而不是一个单词的一部分,所以你可以用like operator轻松地做

您可以通过3个案例找到一个单词

  1. “space'WORD
  2. WORD'space”
  3. 'space'WORD'space'

SELECT * FROM TABLE WHERE字段类似'an'OR字段就像'an'OR字段就像'an'

希望能帮助到你


0
投票

通过CHARINDEX函数在MS SQL Server中完美地完成它(它是MS SQL的内部函数):

if CHARINDEX('an ',@mainString) > 0
begin
    --do something
end

之前在another post中显示了该解决方案。


0
投票

Luka提到的三个案例:

  1. 词之前的空间
  2. 一字接一句
  3. 词之前和之后的空间

要完成此任务,您将编写如下搜索整个单词的查询,并填充表达式以使用前导和尾随空格进行搜索,以捕获表达式开头/结尾的单词:

注意:我使用了一个人为的例子来使这个便携和可证明。

select
  t.txt
from (
  select
    'this is an awesome test of awesomeness man' as txt
) t
where
  charindex(' an ', ' ' + t.txt + ' ') > 0;
© www.soinside.com 2019 - 2024. All rights reserved.