字符包含问题
有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
goif object_id('test') is not nulldrop table testgocreate table test(HM varchar(20))goinsert testselect '01 02 04 05' union allselect '01 02 05 06' union allselect '03 08 09 22'--如果是查询03字符存在的字段select * from testwhere CHARINDEX(' '+'03'+' ',' '+HM+' ')=0/*HM01 02 04 0501 02 05 06*/--如果是查询01,02,05存在的字段select * from testwhere CHARINDEX(' '+'01'+' ',' '+HM+' ')>0 and CHARINDEX(' '+'02'+' ',' '+HM+' ')>0 and CHARINDEX(' '+'05'+' ',' '+HM+' ')>0/*HM01 02 04 0501 02 05 06*/
[解决办法]
select * from tb where charindex('01',HM)>0
and charindex('02',HM)>0
and charindex('05',HM)>0