declare @t table(NAME varchar(10), INPUTCODE varchar(50))
insert @t
select '大白菜', 'dac,bc,101 ' union all
select '西红柿', 'bhs,YSZ,102' union all
select '黄瓜', 'hg,,' union all
select '青椒', 'qj,,lj'
select * from @t
where charindex('b', left(INPUTCODE, charindex(',', INPUTCODE)-1))<>0
=====
lsqkeke,你这个检索,只是对第一个“,”分隔号前的内容进行了检索;比如我修改成上面的测试数据后,就没有检索出【'大白菜', 'dac,bc,101'】这条记录来。
declare @t table(NAME varchar(10), INPUTCODE varchar(50))
insert @t
select '大白菜', 'dbc,bc,101 ' union all
select '西红柿', 'bhs,YSZ,102' union all
select '黄瓜', 'hg,,' union all
select '青椒', 'qj,,lj'
select * from @t
where charindex('b',left(INPUTCODE,charindex(',',INPUTCODE)-1))<>0
参考了http://community.csdn.net/Expert/topic/4598/4598667.xml?temp=.840542这个帖子,查阅CHARINDEX时,发现用PATINDEX可以解决这个问题;如下:
select * from opr.gbcodes where patindex('%b%', inputcode)<>0