Home > Net >  How can I use LIKE with @value in SQL Server?
How can I use LIKE with @value in SQL Server?

Time:10-22

I don't know if this question has been asked before. I searched but I couldn't find the answer.

In short, I want to do something in MsSQL like:

DECLARE @VALUE VARCHAR(2000) = 'Joe and Bill and Michael are my friends'   
SELECT * FROM my_friends WHERE my_friends.name LIKE %@VALUE%

I want to get records from my_friends table which "name" column is in @VALUE string. For this case, Joe, Bill and Michael.

CodePudding user response:

possibly something like this

SELECT * FROM my_friends WHERE my_friends.name LIKE '%' @VALUE '%'

CodePudding user response:

Since your name is meant to be a substring of your variable, you want

WHERE @VALUE like '%' name '%'

You could also use charindex if you want case sensitive matches

WHERE charindex(names,@VALUE)>0
  •  Tags:  
  • sql
  • Related