kiLLua kiLLua - 1 month ago 7x
SQL Question

how to find the specific name in DB from search textbox?

I have a textbox that stands as a search box:

Here is my code example:

Dim txt As String = txt_find.Text
query = "SELECT empID AS 'ID', fname + ' ' + mname + ' ' + lname AS 'Name' FROM empInfo "
query &= "WHERE empID LIKE '%" & txt & "%' OR fname LIKE '%" & txt & "%' OR lname LIKE '%" & txt & "%' OR mname LIKE '%" & txt & "%'"

It returns the correct value if the user entered just first name or last name of the employee.


Input: 28 Or Dennis Or Smith

But when the user Inputs the full name(e.g. Dennis Smith) the result returns nothing.

how should it be done?.


You could try:

"WHERE fname+' '+lname like '%" & txt & "' OR  fname+' '+mname+' '+lname like '%" & txt & "'"

That's just the idea, you just have to include there the possible name formats like LastName, FirstName MiddleName or if you can think of more, the better you can handle your search.