Is there a combination of “LIKE” and “IN” in SQL?

后端 未结 25 1617
灰色年华
灰色年华 2020-11-22 03:08

In SQL I (sadly) often have to use \"LIKE\" conditions due to databases that violate nearly every rule of normalization. I can\'t change that right now. But tha

相关标签:
25条回答
  • 2020-11-22 03:49

    For Sql Server you can resort to Dynamic SQL.

    Most of the time in such situations you have the parameter of IN clause based on some data from database.

    The example below is a little "forced", but this can match various real cases found in legacy databases.

    Suppose you have table Persons where person names are stored in a single field PersonName as FirstName + ' ' + LastName. You need to select all persons from a list of first names, stored in field NameToSelect in table NamesToSelect, plus some additional criteria (like filtered on gender, birth date, etc)

    You can do it as follows

    -- @gender is nchar(1), @birthDate is date 
    
    declare 
      @sql nvarchar(MAX),
      @subWhere nvarchar(MAX)
      @params nvarchar(MAX)
    
    -- prepare the where sub-clause to cover LIKE IN (...)
    -- it will actually generate where clause PersonName Like 'param1%' or PersonName Like 'param2%' or ...   
    set @subWhere = STUFF(
      (
        SELECT ' OR PersonName like ''' + [NameToSelect] + '%''' 
            FROM [NamesToSelect] t FOR XML PATH('')
      ), 1, 4, '')
    
    -- create the dynamic SQL
    set @sql ='select 
          PersonName
          ,Gender
          ,BirstDate    -- and other field here         
      from [Persons]
      where 
        Gender = @gender
        AND BirthDate = @birthDate
        AND (' + @subWhere + ')'
    
    set @params = ' @gender nchar(1),
      @birthDate Date'     
    
    EXECUTE sp_executesql @sql, @params,    
      @gender,  
      @birthDate
    
    0 讨论(0)
  • 2020-11-22 03:49

    No answer like this:

    SELECT * FROM table WHERE something LIKE ('bla% %foo% batz%')
    

    In oracle no problem.

    0 讨论(0)
  • 2020-11-22 03:52

    I was also wondering for something like that. I just tested using a combination of SUBSTRING and IN and it is an effective solution for this kind of problem. Try the below query :

    Select * from TB_YOUR T1 Where SUBSTRING(T1.Something, 1,3) IN ('bla', 'foo', 'batz')
    
    0 讨论(0)
  • 2020-11-22 03:53

    Use an inner join instead:

    SELECT ...
    FROM SomeTable
    JOIN
    (SELECT 'bla%' AS Pattern 
    UNION ALL SELECT '%foo%'
    UNION ALL SELECT 'batz%'
    UNION ALL SELECT 'abc'
    ) AS Patterns
    ON SomeTable.SomeColumn LIKE Patterns.Pattern
    
    0 讨论(0)
  • 2020-11-22 03:53

    do this

    WHERE something + '%' in ('bla', 'foo', 'batz')
    OR '%' + something + '%' in ('tra', 'la', 'la')
    

    or

    WHERE something + '%' in (select col from table where ....)
    
    0 讨论(0)
  • 2020-11-22 03:54

    Sorry for dredging up an old post, but it has a lot of views. I faced a similar problem this week and came up with this pattern:

    declare @example table ( sampletext varchar( 50 ) );
    
    insert @example values 
    ( 'The quick brown fox jumped over the lazy dog.' ),
    ( 'Ask not what your country can do for you.' ),
    ( 'Cupcakes are the new hotness.' );
    
    declare @filter table ( searchtext varchar( 50 ) );
    
    insert @filter values
    ( 'lazy' ),
    ( 'hotness' ),
    ( 'cupcakes' );
    
    -- Expect to get rows 1 and 3, but no duplication from Cupcakes and Hotness
    select * 
    from @example e
    where exists ( select * from @filter f where e.sampletext like '%' + searchtext + '%' )
    

    Exists() works a little better than join, IMO, because it just tests each record in the set, but doesn't cause duplication if there are multiple matches.

    0 讨论(0)
提交回复
热议问题