Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Queries using LIKE wildcards in sql server

Tags:

sql

sql-server

I want to perform a small SQL server search in my ASP.NET web project. My database is not big so I think it's better not to use full-text-search.

I want to perform a simple search like this:

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

I can use = in the following way:

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

but how can I use a variable instead of %something% in the LIKE phrase? Is this correct:

LIKE '"+%myVariable%+"'?

like image 273
Ali_dotNet Avatar asked Oct 18 '11 08:10

Ali_dotNet


People also ask

What are two main wildcards you can use in a like in SQL?

You can use the % and _ wildcards with the SQL LIKE statement to compare values from a SQL table. Here is the basic syntax for the SQL Like statement. The % matches zero, one or more characters while the _ matches a single character.

What are the wildcard characters that are used with the like command?

The wildcard, underscore, is for matching any single character.

How do you write a like statement in a query?

Two main rules of Like Operator are as follows: [ % ] – Represents zero, one, or multiple characters. [ _ ] – Represents one single character.

How use multiple values in SQL with like?

The SQL LIKE clause is used to compare a value to similar values using wildcard operators. There are two wildcards used in conjunction with the LIKE operator. The percent sign represents zero, one or multiple characters. The underscore represents a single number or character.


2 Answers

Use:

where columnA LIKE '%' + myVariable + '%'
like image 162
Aziz Shaikh Avatar answered Oct 14 '22 14:10

Aziz Shaikh


WHERE
columnName LIKE '%' + myVarCharVariable +'%'
like image 31
sll Avatar answered Oct 14 '22 13:10

sll