首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >返回在全文搜索中找到的短语的周围文本,SQL 2005

返回在全文搜索中找到的短语的周围文本,SQL 2005
EN

Stack Overflow用户
提问于 2009-04-08 15:06:34
回答 2查看 1.1K关注 0票数 3

我正在使用contains谓词在SQL Server索引文本字段中查找短语。有没有办法返回文本字段中包含搜索短语的部分,或者它周围的某个区域?

例如,如果我在葛底斯堡演讲(摘录如下)中搜索“人人生而平等”,我想返回“致力于人人生而平等的主张”,例如它周围的一些文本。

代码语言:javascript
运行
复制
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. 
EN

回答 2

Stack Overflow用户

发布于 2009-04-08 15:12:33

嗯,我不熟悉SQL Server sintax,但您可以找到字段中的匹配项并返回它的子字符串。伪码

代码语言:javascript
运行
复制
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个字符的字符串,因此应该可以这样做。

票数 2
EN

Stack Overflow用户

发布于 2014-10-09 07:43:01

只是在尝试完成类似的事情时偶然发现了这一点。基于Seb的响应,我实现了以下内容来解决我们的问题:

代码语言:javascript
运行
复制
SELECT '...' + SUBSTRING(@TextToParse, CHARINDEX(@TheKeyword, @TextToParse)-150, 350) + '...'

这将返回前面有150个字符的关键字或短语。总共返回350个字符。根据需要修改这些数字。此外,省略号也包含在开头和结尾,因为此代码不支持避免单词中间的中断。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/730449

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档