Is it possible to use LIKE and IN for a WHERE statment?

后端 未结 4 1671
后悔当初
后悔当初 2020-12-06 17:47

I have a list of place names and would like to match them to records in a sql database the problem is the properties have reference numbers after there name. eg. \'Ballymena

4条回答
  •  攒了一身酷
    2020-12-06 18:01

    you could use OR

    WHERE 
      dbo.[Places].[Name] LIKE 'Ballymena%' 
      OR dbo.[Places].[Name] LIKE 'Banger%'
    

    or split the string at the space, if the places.name is always in the same format.

    WHERE SUBSTRING(dbo.[Places].[Name], 1, CHARINDEX(dbo.[Places].[Name], ' ')) 
      IN ('Ballymena', 'Banger')
    

    This might decrease performance, because the database may be able to use indexes with like (if the wildcard is at the end you have even a better chance) but most probably not when using substring.

提交回复
热议问题