What is the correct syntax for this query?
var l=db.Fetch<article>("SELECT * FROM articles WHERE title LIKE '%@0%'", 'something');
Or should I use CHARINDEX
?
The SQL LIKE Operator The LIKE operator is used in a WHERE clause to search for a specified pattern in a column. There are two wildcards often used in conjunction with the LIKE operator: The percent sign (%) represents zero, one, or multiple characters. The underscore sign (_) represents one, single character.
The SQL LIKE operator is only applied on a field of types CHAR or VARCHAR to match a pattern. To match a pattern from a word, special characters, and wildcards characters may have used with LIKE operator. The LIKE operator can be used within any valid SQL statement, such as SELECT, INSERT INTO, UPDATE or DELETE.
LIKE is what you would use in a search query. It also allows wildcards like _ (simple character wildcard) and % (multi-character wildcard). = should be used if you want exact matches and it will be faster. Save this answer.
Example. Other than the difference of having wildcard characters, %, and _, there is a significant difference between LIKE and = operators is that LIKE operator does not ignore the trailing spaces while = operator ignores the trailing spaces.
May be
var l=db.Fetch<article>("SELECT * FROM articles WHERE title LIKE @0", "%something%");
I haven't tried this, but I think it is worth trying:
var l=db.Fetch<article>("SELECT * FROM articles WHERE title LIKE @0", "%" + "something" + "%");
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