Home  >  Article  >  Database  >  sql server数据批量模糊查询

sql server数据批量模糊查询

WBOY
WBOYOriginal
2016-06-07 17:48:312925browse

本实例是根据用户输入的内容,然后我们来利用sql left函数来取字符再进行like或模糊查询自己区间的数据,有需要的朋友简单参考一下。

问题

数据为
ID 串码
1 A0000000001
2 A0000000002
3 A0000000003
4 A0000000004
5 A0000000005
6 A0000000006
7 A0000000007
要求:
给出一个查询范围如
A0000000003,A0000000006


sql执行语句

 

 代码如下 复制代码

create table t10(ID int,串码 varchar(20))

insert into t10
1, 'A0000000001' union all
select 2, 'A0000000002' union all
select 3, 'A0000000003' union all
select 4, 'A0000000004' union all
select 5, 'A0000000005' union all
select 6, 'A0000000006' union all
select 7, 'A0000000007'

-- 程序中输入的串@x
declare @x varchar(50)

select @x='A0000000003,A0000000006'

select * from t10
where 串码 between left(@x,charindex(',',@x)-1)
and substring(@x,charindex(',',@x)+1,20)

ID          串码
----------- --------------------
3           A0000000003
4           A0000000004
5           A0000000005
6           A0000000006

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn