日期:2014-05-18 浏览次数:20406 次
declare @c varchar(8000)
set @c ='1,3,2,5,8,4,9'
Select ID,
Num,
Name
From tablea
Where @c= ''
or @c is Null
or Charindex(','+Ltrim(Num)+',',','+Isnull(@c,'')+',')>0
上面的语句执行后的结果样式如:
ID Num Name
-------------------------------------------
1 .. ..
2 .. ..
3 .. ..
4 .. ..
5 .. ..
8 .. ..
9 .. ..
我想要的结果是:
ID Num Name
-------------------------------------------
1 .. ..
3 .. ..
2 .. ..
5 .. ..
8 .. ..
4 .. ..
9 .. ..
declare @tb table(Num varchar(5)) insert into @tb select '1' insert into @tb select '2' insert into @tb select '3' insert into @tb select '4' insert into @tb select '5' insert into @tb select '6' insert into @tb select '7' insert into @tb select '8' insert into @tb select '9' insert into @tb select '10' declare @c varchar(8000) set @c ='1,3,2,5,8,4,9' Select Num From @tb Where @c= '' or @c is Null or Charindex(','+Ltrim(Num)+',',','+Isnull(@c,'')+',')>0 order by Charindex(','+Ltrim(Num)+',',','+Isnull(@c,'')+',')
------解决方案--------------------
declare @c varchar(8000) set @c ='1,3,2,5,8,4,9' Select ID, Num, Name From tablea Where @c= '' or @c is Null or Charindex(','+Ltrim(Num)+',',','+Isnull(@c,'')+',')>0 order by Charindex(','+Ltrim(Num)+',',','+Isnull(@c,'')+',')
------解决方案--------------------
declare @c varchar(8000)
set @c ='1,3,2,5,8,4,9'
declare @w xml
set @c ='1,3,2,5,8,4,9'
set @c =replace('<b><a>1,3,2,5,8,4,9</a></b>',',','</a><a>')
set @w=@c
SELECT T.c.value('.','int') as ID
FROM @w.nodes('/b/a') T(c)