首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

瓜分字符串查询

2012-07-28 
分割字符串查询另换帖子了,问题不是简单的一说,试过几种方法,split charindex貌似都达不到效果测试数据SQL

分割字符串查询
另换帖子了,问题不是简单的一说,试过几种方法,split charindex 貌似都达不到效果
测试数据

SQL code
insert into demoselect 3,'aa','1001' union all select 4,'bb','2001' union allselect 5, 'cc','3001' union allselect 6, '分类1','4001' union allselect 7, '分类2','5001' union allselect 8,'分类','2002'


查询条件declare @str='分类,2' 字段name和code like @str

查询结果应该是
id name code
----------- -------------------- --------------------
4 bb 2001
6 分类1 4001
7 分类2 5001
8 分类 2002

[解决办法]
SQL code
create table demo (id int,name nvarchar(32),code varchar(32))insert into demoselect 3,'aa','1001' union all select 4,'bb','2001' union allselect 5, 'cc','3001' union allselect 6, '分类1','4001' union allselect 7, '分类2','5001' union allselect 8,'分类','2002'declare  @str nvarchar(32)=N'分类,2'declare @str1 nvarchar(32),@str2 nvarchar(32)select @str1=PARSENAME(REPLACE(@str,',','.'),2),@str2=PARSENAME(REPLACE(@str,',','.'),1)select * from demo where (name like '%'+@str1+'%' or name like '%'+@str2+'%' or code like '%'+@str1+'%' or code like '%'+@str2+'%')/*id    name    code4    bb    20016    分类1    40017    分类2    50018    分类    2002*/
[解决办法]
探讨
另换帖子了,问题不是简单的一说,试过几种方法,split charindex 貌似都达不到效果
测试数据

SQL code


insert into demo
select 3,'aa','1001' union all
select 4,'bb','2001' union all
select 5, 'cc','3001' union all
select 6, '分类……

[解决办法]
那帖子树锅已修改。LIKE 方向已经改了.

热点排行