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

后端 未结 25 1614
灰色年华
灰色年华 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:48

    I would suggest using a TableValue user function if you'd like to encapsulate the Inner Join or temp table techniques shown above. This would allow it to read a bit more clearly.

    After using the split function defined at: http://www.logiclabz.com/sql-server/split-function-in-sql-server-to-break-comma-separated-strings-into-table.aspx

    we can write the following based on a table I created called "Fish" (int id, varchar(50) Name)

    SELECT Fish.* from Fish 
        JOIN dbo.Split('%ass,%e%',',') as Splits 
        on Name like Splits.items  //items is the name of the output column from the split function.
    

    Outputs

    1   Bass
    2   Pike
    7   Angler
    8   Walleye
    

提交回复
热议问题