关于sql charindex查询问题

jjcl521 2013-12-11 10:39:22



create table test
(
Id int identity,
DepartID varchar(500)

)
--DepartID 插入部门id,以为,作为分隔
go

INSERT INTO test (DepartID)

select '1,2,3' union
select '1,2,4' union
select '11,12,13' union
select '11,12,14' union
select '11,12,14' union
select '11,22,33'

--如果取部门ID包含为2的
select * from test where charindex('2,',DepartID)>0
--返回所有记录
--想要的结果是只返回,前两条记录
...全文
219 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
Masher 2013-12-11
  • 打赏
  • 举报
回复
据说like 和charindex的效率是一样的。。。
jjcl521 2013-12-11
  • 打赏
  • 举报
回复
引用 10 楼 yupeigu 的回复:
[quote=引用 9 楼 jjcl521 的回复:]

--高手是否还在

select * from test 
 where charindex(',22,',','+DepartID+',')>0

返回结果 (0 行受影响)

哦,我执行了,是可以的,你看: --如果取部门ID包含为2的 select * from test where charindex(','+'22,',','+DepartID+',')>0 [/quote] 是我没有插入 11,22,33这一行数据 结贴
LongRui888 2013-12-11
  • 打赏
  • 举报
回复
引用 9 楼 jjcl521 的回复:

--高手是否还在

select * from test
where charindex(',22,',','+DepartID+',')>0

返回结果 (0 行受影响)



哦,我执行了,是可以的,你看:

--如果取部门ID包含为2的
select * from test where charindex(','+'22,',','+DepartID+',')>0

jjcl521 2013-12-11
  • 打赏
  • 举报
回复

--高手是否还在

select * from test 
 where charindex(',22,',','+DepartID+',')>0

返回结果 (0 行受影响)

jjcl521 2013-12-11
  • 打赏
  • 举报
回复

--3楼也可以 

--只是中间加好稍显多余

--我也只想到这种方法
select * from #test where charindex('1,',DepartID)>0

--没想到前后都加逗号,还是有差距啊
jjcl521 2013-12-11
  • 打赏
  • 举报
回复

--以上回复经过测试,4楼,5楼可行

--其余要是返回为1的就会有问题

select * from #test where charindex(',1,',DepartID)>0
select * from #test where charindex('1,',DepartID)>0

Landa_Jimmy 2013-12-11
  • 打赏
  • 举报
回复



create table #test
(
	Id int identity,
	DepartID varchar(500) 

)
--DepartID 插入部门id,以为,作为分隔 
go 

INSERT INTO #test (DepartID)

select  '1,2,3' union 
select  '1,2,4' union 
select  '11,12,13' union 
select  '11,12,14' union 
select  '11,12,14'  union
select  '11,22,33' 

--如果取部门ID包含为2的
select * from #test where charindex(',2,',DepartID)>0
--返回所有记录
--想要的结果是只返回,前两条记录

Id          DepartID
----------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1           1,2,3
2           1,2,4

(2 行受影响)
Andy__Huang 2013-12-11
  • 打赏
  • 举报
回复

create table test
(
	Id int identity,
	DepartID varchar(500) 

)
--DepartID 插入部门id,以为,作为分隔 
go 

INSERT INTO test (DepartID)
select  '1,2,3' union 
select  '1,2,4' union 
select  '11,12,13' union 
select  '11,12,14' union 
select  '11,12,14'  union
select  '11,22,33' 

select * from test where charindex(',2,',','+DepartID+',')>0

/*
Id	DepartID
1	1,2,3
2	1,2,4
*/
唐诗三百首 2013-12-11
  • 打赏
  • 举报
回复

select * from test 
 where charindex(',2,',','+DepartID+',')>0

/*
Id          DepartID
----------- --------------------------------------------------
1           1,2,3
2           1,2,4

(2 row(s) affected)
*/
LongRui888 2013-12-11
  • 打赏
  • 举报
回复
改成这样就行:
create table test
(
	Id int identity,
	DepartID varchar(500) 

)
--DepartID 插入部门id,以为,作为分隔 
go 

INSERT INTO test (DepartID)

select  '1,2,3' union 
select  '1,2,4' union 
select  '11,12,13' union 
select  '11,12,14' union 
select  '11,12,14'  union
select  '11,22,33' 

--如果取部门ID包含为2的
select * from test where charindex(','+'2,',','+DepartID+',')>0
/*
Id	DepartID
1	1,2,3
2	1,2,4
*/
--返回所有记录
--想要的结果是只返回,前两条记录
KeepSayingNo 2013-12-11
  • 打赏
  • 举报
回复

select top 2 * from #test where charindex('2,',DepartID)>0
Yole 2013-12-11
  • 打赏
  • 举报
回复
引用 楼主 jjcl521 的回复:



create table test
(
	Id int identity,
	DepartID varchar(500) 

)
--DepartID 插入部门id,以为,作为分隔 
go 

INSERT INTO test (DepartID)

select  '1,2,3' union 
select  '1,2,4' union 
select  '11,12,13' union 
select  '11,12,14' union 
select  '11,12,14'  union
select  '11,22,33' 

--如果取部门ID包含为2的
select * from test where charindex('2,',DepartID)>0
--返回所有记录
--想要的结果是只返回,前两条记录


select * from test1 where charindex(',2,',DepartID)>0
Id DepartID ----------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 1 1,2,3 2 1,2,4 (2 行受影响)

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧