SQL search column where one item in column is substring of another item

前端 未结 3 1620
终归单人心
终归单人心 2021-01-16 09:16

Is there a way to for an sql statement to search if a column string with multiple items contains a certain item, but not include a certain item that is a substring. The fol

相关标签:
3条回答
  • 2021-01-16 09:57

    If you want to require that an item be surrounded by spaces you can add them on either side of your list as well as your term:

    select * 
    from tbltest 
    where ' '+platform+' ' like '% item %'
    

    Ideally data is not stored in lists, as this searching will not be terribly efficient.

    0 讨论(0)
  • 2021-01-16 10:11

    I would do something like this:

    select *
    from tbltest
    where platform like '%item%'
    and platform not like '%item.%' -- searching for ASP
    and platform not like '%.item%' -- searching for NET
    

    Notice the additional dot before and after item.

    0 讨论(0)
  • 2021-01-16 10:18
    select * 
    from tbltest
    where (platform like 'item%' or platform like '% item%') 
    and (platform like '%item' or platform like '%item %')
    

    What this does is check whether the item is surrounded by spaces, the beginning and/or the ending of the string.

    A requirement would be that there's no item with a space and you always split on a space. Otherwise you'd need another char to split on.

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