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%+"'
?
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.
The wildcard, underscore, is for matching any single character.
Two main rules of Like Operator are as follows: [ % ] – Represents zero, one, or multiple characters. [ _ ] – Represents one single character.
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.
Use:
where columnA LIKE '%' + myVariable + '%'
WHERE
columnName LIKE '%' + myVarCharVariable +'%'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With