当前位置: 代码迷 >> Sql Server >> SQLServer给出查询数据范围,实现批量模糊查询,该如何解决
  详细解决方案

SQLServer给出查询数据范围,实现批量模糊查询,该如何解决

热度:36   发布时间:2016-04-27 17:59:44.0
SQLServer给出查询数据范围,实现批量模糊查询
数据为
ID 串码
1 A0000000001
2 A0000000002
3 A0000000003
4 A0000000004
5 A0000000005
6 A0000000006
7 A0000000007
要求:
给出一个查询范围如
A0000000003,A0000000006
然后查询这两条数据范围之间的全部数据,要求数据一个文本框内录入,中间使用半角逗号,
有没有什么简便方法来实现这样的模糊批量查询?
不胜感激;

------解决方案--------------------
A0000000003,A0000000006

建议放到两个文本框中输入,否则还要拆分字符串

where 串码 between 'A0000000003' and 'A0000000006'
------解决方案--------------------
SQL code
declare @s varchar(50)select @s='A0000000003,A0000000006'select * from tb where 串码 between left(@x,charindex(',',@s)-1)and substring(@s,charindex(',',@s)+1,20)
------解决方案--------------------
SQL code
create table t10(ID int,串码 varchar(20))insert into t10select 1, 'A0000000001' union allselect 2, 'A0000000002' union allselect 3, 'A0000000003' union allselect 4, 'A0000000004' union allselect 5, 'A0000000005' union allselect 6, 'A0000000006' union allselect 7, 'A0000000007'-- [email protected]declare @x varchar(50)select @x='A0000000003,A0000000006'select * from t10where 串码 between left(@x,charindex(',',@x)-1)and substring(@x,charindex(',',@x)+1,20)ID          串码----------- --------------------3           A00000000034           A00000000045           A00000000056           A0000000006
------解决方案--------------------
探讨
上面的回答很精彩,
我现在有一个新的想法
可否使用Like进行模糊查询其中的范围?
客户输入 a100000,a2dafdsfs
查询 where serialNumber like 'a100000,a2dafdsfs'
where serialNumber like '%a100000,a2dafdsfs%'
来实现上面的功能?
  相关解决方案