日期:2014-05-18  浏览次数:20507 次

字符包含问题
有A 表 。 里面有 个 字段 HM 为字符型。 
HM 里面的数据为 

01 02 04 05 
01 02 05 06
03 08 09 22


以 3个数字为例。 01 02 05  

那么需要 用这 3个 数字在 A 表中提出数据 应为:
01 02 04 05 
01 02 05 06

也就是 HM 中 包含 01 02 05 这3个字符。 

请大家帮下忙看一看怎么的写哦

------解决方案--------------------
SQL code

go
if object_id('test') is not null
drop table test
go
create table test(
HM varchar(20)
)
go
insert test
select '01 02 04 05' union all
select '01 02 05 06' union all
select '03 08 09 22'

--如果是查询03字符存在的字段
select * from test
where CHARINDEX(' '+'03'+' ',' '+HM+' ')=0
/*
HM
01 02 04 05
01 02 05 06
*/
--如果是查询01,02,05存在的字段
select * from test
where CHARINDEX(' '+'01'+' ',' '+HM+' ')>0
      and CHARINDEX(' '+'02'+' ',' '+HM+' ')>0
      and CHARINDEX(' '+'05'+' ',' '+HM+' ')>0
/*
HM
01 02 04 05
01 02 05 06
*/

------解决方案--------------------
select * from tb where charindex('01',HM)>0
and charindex('02',HM)>0
and charindex('05',HM)>0