bh为varchar类型,存储A-Z字符,现在想查询在串中“AA,AC,DE,FA,KO,PQ”出现的每个词的行:select * from table1 where ','+bh+',' like ',AA,AC,DE,FA,KO,PQ,'
结果返回总是空,那里出错了?

解决方案 »

  1.   

    select * from table1 where  ', '+bh+ ', ' like  '%",AA,AC,DE,FA,KO,PQ,"% '不知道行不行,感觉你的语句还是有问题的
      

  2.   

    不明白, 用
    select * from table1 where bh like '%AA,AC,DE,FA,KO,PQ%'
    不就完了么?
      

  3.   

    你不会是要这个东西吧?create table #table
    (ID int identity(1,1), bh char)declare @a char
    set @a= 'A'
    while @a <> 'Z'
    begin
    insert #table values(@a)
    set @a = char(ascii(@a)+1)
    endcreate procedure #getNumbers(@str varchar(256))
    AS
    declare @s varchar(256)
    set @s = replace(@str, ',', '') declare @n int 
    set @n= 1 declare @index int
    set @index = 0
    while @n < len(@s)
    begin
    select @index = ID from #table
    where bh = substring(@s, @n, 1)
    print @index
    set @n = @n +1 
    end
    -----------------------------------------------exec  #getNumbers 'AA,AC,DE,FA,KO,PQ'drop table #table 
      

  4.   

    再说一下我的问题:表table1管理所有设备,BH、MC、CJ、GG...(编号、名称、厂家、规格...)
    其字段BH是设备编号,用字母编号,比如:
    AA  21电视  长红...
    AB  29电视  海尔...
    ....
    BCA  10KVA电机 ...需求:查询时由用户指定设备编号筛选设备,比如用户指定了:“AA,AC,DE,FA,KO,PQ”,要找这6个设备。
    ■回ruanchao :
    我的有什么问题?你的我试了不行。■回Generics:
    你这样不行,因为如果用户指定“AAC,ABC”,你会把AA、AB、等也找出来。
      

  5.   

    知道原因了, where  ', '+bh+ ', ' like  ',AA,AC,DE,FA,KO,PQ, ' 肯定不行。where 条件中怎样判断字段 BH 的内容是不是字符串常数“aa,bb,cc,dac”一个子串呢?
      

  6.   

    select * from table1 where bh like  '%,AA,AC,DE,FA,KO,PQ,%' select * from table1 where bh like '%,AA%' or bh like '%,AC%' or bh like '%,DE%' or bh like '%,FA%' or bh like '%,KO%'  or bh like ',PQ'
      

  7.   

    已解决:
    select * from table1 where charindex(','+bh+',', ',AA,AC,DE,FA,KO,PQ,')>0而  where bh like   '%,AA,AC,DE,FA,KO,PQ,% '  要求bh中必须同时出现 “,AA,AC,DE,FA,KO,PQ, ”,所以总是返回空。