• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • MsSql
  • Mysql
  • oracle
  • MariaDB
  • DB2
  • SQLite
  • PostgreSQL
  • MongoDB
  • Redis
  • Access
  • 数据库其它
  • sybase
  • HBase
您的位置:首页 > 数据库 >Mysql > sql中生成查询的模糊匹配字符串

sql中生成查询的模糊匹配字符串

作者:匿名 字体:[增加 减小] 来源:互联网 时间:2018-12-05

匿名通过本文主要向大家介绍了sql中生成查询的模糊匹配字符串等相关知识,希望本文的分享对您有所帮助

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
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

相关文章

  • 2018-12-05设计数据库的一般步骤及例子
  • 2017-05-11MySQL无法启动几种常见问题小结
  • 2018-12-05sql 语句中的 NULL值
  • 2018-12-05oracle 10g OEM常规错误(Unknown host specified)解决
  • 2018-12-05sqlserver 触发器教程
  • 2018-12-05Visual Studio2017数据库架构介绍
  • 2017-05-11Mysql错误1366 - Incorrect integer value解决方法
  • 2018-12-05提高MySQL数据库的安全性(一)
  • 2018-12-05关于Cast函数的文章推荐
  • 2018-12-05MySQL插入数据时遇到中文乱码改怎么办?

文章分类

  • MsSql
  • Mysql
  • oracle
  • MariaDB
  • DB2
  • SQLite
  • PostgreSQL
  • MongoDB
  • Redis
  • Access
  • 数据库其它
  • sybase
  • HBase

最近更新的内容

    • ERROR 1222 (21000): The used SELECT statements have a differ
    • MySQL 5.0触发器参考教程第1/4页
    • mysql5.7.18在mac os10.12下安装教程步骤
    • mysql binlog二进制日志详解
    • [转]十个 MongoDB 使用要点
    • MySQL rownumber SQL生成自增长序号使用介绍
    • oracle dba 应该熟悉的命令
    • Oracle PL/SQL入门慨述
    • 用MySQL分析SQL的耗时问题
    • Mysql中的Sequence实现方法实例详解

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有