首页 / MYSQL / sql中生成查询的模糊匹配字符串
sql中生成查询的模糊匹配字符串
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了sql中生成查询的模糊匹配字符串,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1793字,纯文字阅读大概需要3分钟。
内容图文
sql中生成查询的模糊匹配字符串
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[f_Sql]') and xtype in (N'FN', N'IF', N'TF'))drop function [dbo].[f_Sql]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[序数表]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [序数表]
GO
--为了效率,所以要一个辅助表配合
select top 1000 id=identity(int,1,1) into 序数表
from syscolumns a,syscolumns b
alter table 序数表 add constraint pk_id_序数表 primary key(id)
go
/*--根据指定字符串生成查询的模糊匹配字符串
条件连接的关键字为 and,or
可以任意指定括号
生成的条件表达式为 like 模糊匹配
--邹建 2004.08(引用请保留此信息)--*/
/*--调用示例
--调用示例
select A=dbo.f_Sql('(Web or HTML or Internet) and (Programmer or Developer)','content')
select B=dbo.f_Sql('Web or HTML or Internet','content')
select C=dbo.f_Sql('(Web and HTML)','content')
select D=dbo.f_Sql('Web','content')
--*/
--示例函数
create function f_Sql(
@str Nvarchar(1000), --要检索的字符串
@fdname sysname --在那个字段中检索
)returns Nvarchar(4000)
as
begin
declare @r Nvarchar(4000)
set @r=''
select @r=@r+case
when substring(@str,id,charindex(' ',@str+' ',id)-id) in('or','and')
then ' '+substring(@str,id,charindex(' ',@str+' ',id)-id)+' '
when substring(@str,id,1)='('
then '(['+@fdname+'] like ''%'
+substring(@str,id+1,charindex(' ',@str+' ',id)-id-1)
+'%'''
when substring(@str,charindex(' ',@str+' ',id)-1,1)=')'
then '['+@fdname+'] like ''%'
+substring(@str,id,charindex(' ',@str+' ',id)-id-1)
+'%'')'
else '['+@fdname+'] like ''%'
+substring(@str,id,charindex(' ',@str+' ',id)-id)
+'%'''
end
from 序数表
where id<=len(@str)
and charindex(' ',' '+@str,id)-id=0
return(@r)
end
go
内容总结
以上是互联网集市为您收集整理的sql中生成查询的模糊匹配字符串全部内容,希望文章能够帮你解决sql中生成查询的模糊匹配字符串所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。