select * from bj_allnewssearchresult where 
(322  in (type)) 
其中type是varchar类型,他的数据格式为
321,322,323
这样的格式我想选择出来包含322的字段出来,却发现出了问题1  如果select * from bj_allnewssearchresult where 
(322  in (type)) 这么写提示:将 varchar 值 '321,322,323' 转换为数据类型为 int 的列时发生语法错误。如果select * from bj_allnewssearchresult where 
('322'  in (type))
却有选择不出来郁闷了,怎么解决?
谢谢各位大虾!!!!!!!

解决方案 »

  1.   

    select * 
    from bj_allnewssearchresult
    where charindex(',322,',','+type+',')>0
      

  2.   

    create table test(col varchar(30))
    insert into test values('321,322,323')
    insert into test values('421,422,423')
    insert into test values('361,362,363')
    go
    select * from test where col like '%322%'
    drop table test
      

  3.   

    出错的原因在于:你的type值为 321,322,323,它是一个字串,不是集合。在用数学知识理解为:
    type="321,322,323"
    而不是
    type:{321,322,323}
    用charindex,patindex,like都可以,不过楼上的like写的有问题,会误选数据出来。create table test(col varchar(30))
    insert into test values('321,322,323')
    insert into test values('421,422322,423')
    insert into test values('361,362,363')
    go
    select * from test where col like '%322%'
    --执行后看能选出什么来
    --应该是
    --select * from test where ','+col+',' like '%,322,%'
    drop table test当然也可以用动态SQL,不过有点小题大做了。