SQL 'LIKE' query using '%' where the search criteria contains '%'

后端 未结 8 1957
被撕碎了的回忆
被撕碎了的回忆 2020-12-03 02:29

I have an SQL query as below.

Select * from table 
where name like \'%\' + search_criteria + \'%\' 

If search_criteria = \'abc\', it will r

8条回答
  •  长情又很酷
    2020-12-03 03:18

    The easiest solution is to dispense with "like" altogether:

    Select * 
    from table
    where charindex(search_criteria, name) > 0
    

    I prefer charindex over like. Historically, it had better performance, but I'm not sure if it makes much of difference now.

提交回复
热议问题