我正在使用包含谓词在SQL Server索引文本字段中查找短语。有没有办法返回包含搜索短语的文本字段部分或其周围的某些区域?
例如,如果我在葛底斯堡地址(摘录如下)中搜索“所有男人都是平等的”,我想回到“致力于所有人都被创造平等的主张”,例如围绕它的一些文字。
Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that *all men are created equal.*
Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure. We are met on a great battle-field of that war. We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live. It is altogether fitting and proper that we should do this.
答案 0 :(得分:2)
好吧,我不熟悉SQL Server sintax,但你可以在字段中找到它并为它返回一个子字符串。的伪代码强>:
SELECT
SUBSTRING(field, MAX(0, STRPOS(field, 'all men are equal' - 20), STRLEN('all men are equal') + 40)
FROM
yourtable
WHERE
field CONTAINS 'all men are equal'
有了这个你只找到包含短语的那些记录的子字符串的位置,并返回一个字符串40个字符更长,所以这样的东西应该有用。
答案 1 :(得分:0)
在尝试完成类似的事情时偶然发现了这一点。基于Seb的回复,我实施了以下解决方案:
SELECT '...' + SUBSTRING(@TextToParse, CHARINDEX(@TheKeyword, @TextToParse)-150, 350) + '...'
这将返回前面带有150个字符的关键字或短语。总共将返回350个字符。根据需要修改这些数字。此外,椭圆包含在开头和结尾,因为这段代码不能避免在单词中间出现断裂。