100求助,sql全文检索,如何返回关键字所在的那段文字

simen_frankly 2009-04-21 10:00:26
用sql全文检索时,如何返回关键字所在的那段文字,而不是整条记录,谢谢了
...全文
456 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
simen_frankly 2009-05-06
  • 打赏
  • 举报
回复
还没解决,帮忙啊
deng520159 2009-04-24
  • 打赏
  • 举报
回复
顶一下,学习一下,
陌上花花 2009-04-24
  • 打赏
  • 举报
回复
帮顶了。顺便学习了
tailor2 2009-04-24
  • 打赏
  • 举报
回复
mark
sushou2009 2009-04-24
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wxg22526451 的回复:]
SQL code--> Test Data: [T1]
if object_id('[T1]') is not null drop table [T1]
create table [T1] ([编号] int,[内容] varchar(5))
insert into [T1]
select 1,'asdf' union all
select 1,'ewrer' union all
select 2,'12123' union all
select 2,'weew'
if object_id('[T2]') is not null drop table [T2]
create table [T2] ([编号] int,[内容2] varchar(5))
insert into [T2]
select 1,'abbb' union all
selec…
[/Quote]
顶!有耐心!
simen_frankly 2009-04-24
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 wesleyluo 的回复:]
这里一定要踩一脚。
从上面看出来两种思路:
12.直接取找到的那字符的前N个和后N个字符;
都好强大啊。
[/Quote]
没有更好的方法吗?“直接取找到的那句话,也就是通过分隔符来截断”效率怎么样呢?很担心效率啊
wesleyluo 2009-04-22
  • 打赏
  • 举报
回复
这里一定要踩一脚。
从上面看出来两种思路:
1.直接取找到的那句话,也就是通过分隔符来截断;
2.直接取找到的那字符的前N个和后N个字符;
都好强大啊。
xiaofangyanan 2009-04-22
  • 打赏
  • 举报
回复
学习中,还是帮楼主顶下
zhoche2008 2009-04-22
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 simen_frankly 的回复:]
up
[/Quote]CustcardKeys
zzxap 2009-04-21
  • 打赏
  • 举报
回复
[code=SQL]
自定义取出第几个分割字符前的字符串,默认位置(0)
格式:dbo.split(字段名,'分隔字符',取出的第几个字符串)
如果没有分隔的字符,则返回整个字符串。
如果取出的位置字符串的位置超出Index则返回空。

CREATE FUNCTION [dbo].[split]
(@str nvarchar(4000),@code varchar(10),@no int )
RETURNS varchar(200)
AS
BEGIN

declare @intLen int
declare @count int
declare @indexb int
declare @indexe int
set @intLen=len(@code)
set @count=0
set @indexb=1


if @no=0
if charindex(@code,@str,@indexb)<>0
return left(@str,charindex(@code,@str,@indexb)-1)
else
return @str

while charindex(@code,@str,@indexb)<>0
begin
set @count=@count+1
if @count=@no
break
set @indexb=@intLen+charindex(@code,@str,@indexb)
end


if @count=@no
begin

set @indexe=@intLen+charindex(@code,@str,@indexb)
if charindex(@code,@str,@indexe)<>0
return substring(@str,charindex(@code,@str,@indexb)+len(@code),charindex(@code,@str,@indexe)-charindex(@code,@str,@indexb)-len(@code))
else
return right(@str,len(@str)-charindex(@code,@str,@indexb)-len(@code)+1)

end

return ''

END





CREATE FUNCTION [dbo].[split]
(@str nvarchar(4000),@code varchar(10),@no int )
RETURNS varchar(200)
AS
BEGIN

declare @intLen int
declare @count int
declare @indexb int
declare @indexe int
set @intLen=len(@code)
set @count=0
set @indexb=1


if @no=0
if charindex(@code,@str,@indexb)<>0
return left(@str,charindex(@code,@str,@indexb)-1)
else
return @str

while charindex(@code,@str,@indexb)<>0
begin
set @count=@count+1
if @count=@no
break
set @indexb=@intLen+charindex(@code,@str,@indexb)
end


if @count=@no
begin

set @indexe=@intLen+charindex(@code,@str,@indexb)
if charindex(@code,@str,@indexe)<>0
return substring(@str,charindex(@code,@str,@indexb)+len(@code),charindex(@code,@str,@indexe)-charindex(@code,@str,@indexb)-len(@code))
else
return right(@str,len(@str)-charindex(@code,@str,@indexb)-len(@code)+1)

end

return ''

END




---------------------------------


CREATE Function f_trimstr(@str varchar(100))

returns varchar(100)

--功能:去掉字符串中的所有空格

AS

begin

declare @i int
declare @s1 varchar(50)
declare @result varchar(100)
declare @len int

select @result = ''
select @str = ltrim(rtrim(@str))
select @len = len(@str)
select @i = 1

while @i<=@len
begin
select @s1 = substring(@str,@i,1)
if(@s1<>'')
begin
select @result = @result + @s1
end
select @i = @i + 1
end

return @result

end


[/CODE]
zzxap 2009-04-21
  • 打赏
  • 举报
回复
[code=SQL]
create function f_split(@SourceSql varchar(8000),@StrSeprate varchar(10))
returns @temp table(a varchar(100))
--实现split功能 的函数
--date :2003-10-14
as
begin
declare @i int
set @SourceSql=rtrim(ltrim(@SourceSql))
set @i=charindex(@StrSeprate,@SourceSql)
while @i>=1
begin
insert @temp values(left(@SourceSql,@i-1))
set @SourceSql=substring(@SourceSql,@i+1,len(@SourceSql)-@i)
set @i=charindex(@StrSeprate,@SourceSql)
end
if @SourceSql<>''
insert @temp values(@SourceSql)
return
end

返回的是一个table,所以执行要用如下格式:select * from dbo.f_split('ABC:BC:C:D:E',':')

SQL 中是否包含字符用charindex()


[/CODE]
zzxap 2009-04-21
  • 打赏
  • 举报
回复

string sql="select xx from table where xx='"+xxx+"'"
执行以上语句得到一个string str1
然后用符号的正则表达式分拆这个字段。
string[] Array1=str1.split(",");
int a=Array1.length;
string str2=""
for (int i=0,i<a,i++)
{
if (Array1[i].IndexOf('"+xxx+"')>-1 )
{
str2= Array1[i];
}

}


simen_frankly 2009-04-21
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 zzxap 的回复:]
你的意思是

string str="阿圣诞节过后卡技术大会分公司的回复,asdfasdf.asdf.阿斯顿飞嘎斯。"

如果搜索“公司”就返回“阿圣诞节过后卡技术大会分公司的回复”这一段?而不是整段?
[/Quote]

就是这个意思,谢谢
itcrazyman 2009-04-21
  • 打赏
  • 举报
回复
楼主把问题说的详细些
帮顶
冰凝瞬间1986 2009-04-21
  • 打赏
  • 举报
回复
blestcc 2009-04-21
  • 打赏
  • 举报
回复
樓主要不要出來再把問題說清楚些啊
jcyluck 2009-04-21
  • 打赏
  • 举报
回复

zzxap 2009-04-21
  • 打赏
  • 举报
回复
你的意思是

string str="阿圣诞节过后卡技术大会分公司的回复,asdfasdf.asdf.阿斯顿飞嘎斯。"

如果搜索“公司”就返回“阿圣诞节过后卡技术大会分公司的回复”这一段?而不是整段?
wxg22526451 2009-04-21
  • 打赏
  • 举报
回复
--> Test Data: [T1]
if object_id('[T1]') is not null drop table [T1]
create table [T1] ([编号] int,[内容] varchar(5))
insert into [T1]
select 1,'asdf' union all
select 1,'ewrer' union all
select 2,'12123' union all
select 2,'weew'
if object_id('[T2]') is not null drop table [T2]
create table [T2] ([编号] int,[内容2] varchar(5))
insert into [T2]
select 1,'abbb' union all
select 1,'脸' union all
select 2,'人人' union all
select 2,'weew'
--Code
--创建临时表
create table #tmp(所在的表及字段 varchar(100),字段内容 text)
go
create proc proc_search
@str varchar(100)
as
begin

--declare @str varchar(100)
--set @str='aa' --要搜索的字符串
declare @s varchar(8000)
declare tb cursor local for
select s='if exists(select 1 from ['+b.name+'] where ['+a.name+'] like ''%'+@str+'%'')
insert into #tmp select ''['+b.name+'].['+a.name+']'',['+a.name+'] from ['+b.name+'] where ['+a.name+'] like ''%'+@str+'%''
'
from syscolumns a join sysobjects b on a.id=b.id
where b.xtype='U' and a.status>=0
and a.xusertype in(175,239,231,167)
open tb
fetch next from tb into @s
while @@fetch_status=0
begin
exec(@s)
fetch next from tb into @s
end
close tb
deallocate tb

select * from #tmp
end
go
--执行

exec proc_search 'a'
--Drop
drop table T1
drop table T2
drop table #tmp
drop proc proc_search
--Result
/*
所在的表及字段 字段内容
---------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
[T1].[内容] asdf
[T2].[内容2] abbb
*/
sushou2009 2009-04-21
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 zzxap 的回复:]
SQL code
自定义取出第几个分割字符前的字符串,默认位置(0)
格式:dbo.split(字段名,'分隔字符',取出的第几个字符串)
如果没有分隔的字符,则返回整个字符串。
如果取出的位置字符串的位置超出Index则返回空。

CREATE FUNCTION [dbo].[split]
(@str nvarchar(4000),@code varchar(10),@no int )
RETURNS varchar(200)
AS
BEGIN

declare @intLen int
declare @count int
declare @indexb int
declare …
[/Quote]
zzxap解答的不错很有耐心
加载更多回复(6)

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧