我有一个文字,
'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')
这是一种方法。
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 %'
;
有一些方法可以做到这一点,似乎你想找到一个单词而不是一个单词的一部分,所以你可以用like operator
轻松地做
您可以通过3个案例找到一个单词
SELECT * FROM TABLE WHERE字段类似'an'OR字段就像'an'OR字段就像'an'
希望能帮助到你
通过CHARINDEX函数在MS SQL Server中完美地完成它(它是MS SQL的内部函数):
if CHARINDEX('an ',@mainString) > 0
begin
--do something
end
之前在another post中显示了该解决方案。
Luka提到的三个案例:
要完成此任务,您将编写如下搜索整个单词的查询,并填充表达式以使用前导和尾随空格进行搜索,以捕获表达式开头/结尾的单词:
注意:我使用了一个人为的例子来使这个便携和可证明。
select
t.txt
from (
select
'this is an awesome test of awesomeness man' as txt
) t
where
charindex(' an ', ' ' + t.txt + ' ') > 0;