Queries using LIKE wildcards on sql server

I want to do a small SQL server search in my ASP.NET web project, my database is small, so I think it's better not to use full-text search I want to do a simple search, for example:

select * from mytable where columnA LIKE '%something%' 

I can use = as follows:

 select * from mytable where columnA='"+myVariable+"' 

but how can I use a variable instead of %something% in a LIKE phrase? it is right? LIKE '"+%myVariable%+"'

I am using VS2010, C #

thanks

+6
source share
7 answers

Using:

 where columnA LIKE '%' + myVariable + '%' 
+7
source
 WHERE columnName LIKE '%' + myVarCharVariable +'%' 
+8
source

Try this query:

 select * from tablename where colname like '%' + @varname + '%' 

Hope this helps.

+5
source

I just tried this and found what you can do as shown below:

 SELECT * FROM whatever WHERE column LIKE '%' +@var +'%' 
+4
source
 DECLARE @myVariable varchar(MAX) SET @myVariable = 'WhatYouAreLookingFor' SELECT * FROM mytable WHERE columnA LIKE '%' + @myVariable + '%' 
+1
source

In case someone else stumbles upon this post, like me. In SSMS 2012 with SQL 2012 Server feedback, I was able to use the code without problems as follows.

 Declare @MyVariable Set @MyVariable = '%DesiredString%' Select * From Table_A Where Field_A like @MyVariable 

Then, every time you want to change the desired line, just change it in the Set statement.

I know that this post was made before 2012, so I mention it if someone with a newer installation is watching this post.

+1
source

Ok, you could do something like:

 var query = "SELECT * FROM MyTable WHERE columnA LIKE '%" + myVariable + "%'"; 
0
source

Source: https://habr.com/ru/post/899515/


All Articles