0

I've search and tried a lot of procedure but did not work. CREATE PROCEDURE did not work, SET @variable is not acceptable and a couple more codes. This is my last "query" code that still didn't work.

qry = "select * from employeefile where empfname LIKE '%''" + input + "''%'"

empfname is a name of an employee from table employeefile that possibly consists of three words. The input could be the first word, second word, third word or the entire words.

when i tried to input any word within the name, the program will still prompt, "no records found." when i tried to change the query into

qry = "select * from employeefile where empfname LIKE '%existingname%'"

and my input is "existingname", the program runs just as i want it to.

This code is one of those that i seached but still didn't work.

T-SQL and the WHERE LIKE %Parameter% clause

How to use like clause in MySQL 5.0 Statement

T-SQL and the WHERE LIKE %Parameter% clause

The problem here is when i use a variable... i probably get the wrong way of using it into query. Please help me. I am new here by the way.

Cœur
  • 37,241
  • 25
  • 195
  • 267
MAC
  • 676
  • 3
  • 13
  • 32
  • 1
    thanks guys for the tips. i got the answer. it turns out that i just overdid the single quote. it must be written this way: qry = "select * from employeefile where empfname LIKE '%" + input + "%'" – MAC Feb 25 '15 at 07:19

3 Answers3

3

If I'm understanding your question correctly:

Dim command As New MySqlCommand("SELECT * FROM emplyeefile WHERE empfname LIKE '%' + @empfname + '%'", connection)

command.Parameters.AddWithValue("@empfname", input)

or:

Dim command As New MySqlCommand("SELECT * FROM emplyeefile WHERE empfname LIKE @empfname", connection)

command.Parameters.AddWithValue("@empfname", "%" & input & "%")

You have to concatenate the wildcards with the input text, either in your SQL code or your VB code.

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
0

This is the correct syntax: LIKE '%blah%', but this code does not put the quotes outside the %s: LIKE '%''" + input + "''%'".

Rick James
  • 135,179
  • 13
  • 127
  • 222
-1

i got the answer. it turns out that i just overdid the single quote. it must be written this way:

qry = "select * from employeefile where empfname LIKE '%" + input + "%'"
MAC
  • 676
  • 3
  • 13
  • 32
  • Not using a parameter, therefore bad code, whether it works in some cases or not. – jmcilhinney Mar 20 '15 at 08:15
  • 1
    the above question is not using parameter; therefore, the answer should also not use parameter and the parameter thing will only be a suggestion. For starters, this could solve the LIKE clause problem then when everything turns okay, parameter will be the next issue. @jmcilhinney i hope you get what i mean – MAC Mar 20 '15 at 08:58