Use of SqlParameter in SQL LIKE clause not working
What you want is:
tblCustomerInfo.Info LIKE '%' + @SEARCH + '%'
(or edit the parameter value to include the % in the first place).
Otherwise, you are either (first sample) searching for the literal "@SEARCH" (not the arg-value), or you are embedding some extra quotes into the query (second sample).
In some ways, it might be easier to have the TSQL just use LIKE @SEARCH
, and handle it at the caller:
command.Parameters.AddWithValue("@SEARCH","%" + searchString + "%");
Either approach should work.
Add SqlParameter to bind LIKE '%@x%'
I think @vendor
is being treated as a literal in your query instead of a parameter.
Try defining your query as follows:
string strQuery =
"select TOP 500 * from [mike_db].[dbo].[na_pe_sql_import] where vendname like '%' + @vendor + '%'";
Then add the parameter like this:
cmd.Parameters.AddWithValue("@vendor", search);
LIKE clause not working when used in a HAVING clause
You must use the ESCAPE
character as the square brackets are used for Pattern matching when using LIKE
. The escape character goes in front of any character you want to use the literal value of.
HAVING
should be used for filtering on an aggregate result. WHERE
is for filtering the data before it is aggregated (when you are using aggregates). In your case you should use WHERE
instead of HAVING
.
SELECT campaign_name,
SUM(clicks) AS Total_Clicks
FROM FB_Raw
WHERE campaign_name LIKE '%![PROS!]%' ESCAPE '!'
GROUP BY campaign_name;
SQL Parameter not working in Select .... Like statement
You need to concatenate wildcard:
string sql = "Select Top 5 ExamName From Qualifications Where ExamName Like '%' + @ExamName + '%'";
If you use SQL Server 2012+ use:
string sql = "Select Top 5 ExamName From Qualifications Where ExamName Like CONCAT('%', @ExamName, '%')";
Using: '%@ExamName%'
you search for exact string. I guess you don't have value in column = '%@ExamName%'
Warning:
Keep in mind that
%phrase%
is not SARG-able so query optimizer won't use index on that column (if exists any).Using
TOP
without explicitORDER BY
may return different results between multiple executions.
Howto? Parameters and LIKE statement SQL
Your visual basic code would look something like this:
Dim cmd as New SqlCommand("SELECT * FROM compliance_corner WHERE (body LIKE '%' + @query + '%') OR (title LIKE '%' + @query + '%')")
cmd.Parameters.Add("@query", searchString)
LIKE' not working in SQL statement in code, but is in SSMS
The issue here is the parameter substitution. Try treating the value just like a string constant:
WHERE firstName LIKE '%' + @name + '%'
Note: No single quotes.
Or, alternatively, put the wildcards in in the application:
WHERE firstName LIKE @name
T-SQL and the WHERE LIKE %Parameter% clause
It should be:
...
WHERE LastName LIKE '%' + @LastName + '%';
Instead of:
...
WHERE LastName LIKE '%@LastName%'
Related Topics
Put Wpf Control into a Windows Forms Form
Htmlagilitypack -- Does <Form> Close Itself for Some Reason
Accessing Properties Through Generic Type Parameter
Performing Inserts and Updates with Dapper
Write to Windows Application Event Log Without Event Source Registration
Differencebetween Casting and Conversion
How to Check If Variable's Type Matches Type Stored in a Variable
Adding Stored Procedures Complex Types in Entity Framework
Async/Await Different Thread Id
Displaying Standard Datatables in MVC
ASP.NET 2012 Unobtrusive Validation with Jquery
Wpf Databinding to Interface and Not Actual Object - Casting Possible
Change Color and Font for Some Part of Text in Wpf C#
Watermark in System.Windows.Forms.Textbox