954,551 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Multiple LIKE separated by OR

I could use some help with a SQL SELECT LIKE command. I have two TextBox'es that I am using as input for two different searches through a SQL database with the result placed in a GridView.

It is important to note that the search can be performed from either TextBox, so I wanted to use the LIKE command separated by an OR expression. But it does not seem to work. I always need to place something in BOTH TextBox'es to get a result.

I am a novice at VB programming and SQL databases. So any assistance is warmly accepted.

Thanks!

William Aldrich-Thorpe

wthorpe
Newbie Poster
2 posts since Jun 2006
Reputation Points: 10
Solved Threads: 0
 

Hi Willem,

Can Textbox1 and Textbox2 contain a value at the same time , or only one of them at the same time?

If textbox1.text<>"" and textbox2<>"" then
strQuery = "SELECT * FROM Table WHERE
Field LIKE %" & Textbox1.text & "% OR Field
LIKE %" & Textbox2.text & "%"
elseif Textbox1.text<>"" end Textbox2.text="" then
strQuery = "SELECT * FROM Table WHERE
Field LIKE %" & Textbox1.text & "%
elseif Textbox1.text="" end Textbox2.text<>"" then
strQuery = "SELECT * FROM Table WHERE
Field LIKE %" & Textbox2.text & "%
endif

This will do the job.

If you don't need all fields from the table, replace the * for the fieldnames separated bij a ,
This will speedup yor query when using larger databases.

Grtz,
Nokkieja

Nokkieja
Newbie Poster
3 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 
Hi Willem, Can Textbox1 and Textbox2 contain a value at the same time , or only one of them at the same time? If textbox1.text<>"" and textbox2<>"" then strQuery = "SELECT * FROM Table WHERE Field LIKE %" & Textbox1.text & "% OR Field LIKE %" & Textbox2.text & "%" elseif Textbox1.text<>"" end Textbox2.text="" then strQuery = "SELECT * FROM Table WHERE Field LIKE %" & Textbox1.text & "% elseif Textbox1.text="" end Textbox2.text<>"" then strQuery = "SELECT * FROM Table WHERE Field LIKE %" & Textbox2.text & "% endif This will do the job. If you don't need all fields from the table, replace the * for the fieldnames separated bij a , This will speedup yor query when using larger databases. Grtz, Nokkieja



Thank you for the quick reply. I can see how this will work. However, I am building a web application using ASP and VB. I am unsure how to make this fit within those constraints. As I I stated previously I am a mere novice in all of this. Here is a sample of what I see:

My Default.ASPX code sample (just a couple of lines):"
SelectCommand="SELECT OneViewName, OneViewID, AccountMD, ProposalDueDate FROM GeneralData WHERE (OneViewName LIKE '%' + @OneViewName + '%')">
[FONT='Verdana','sans-serif']
[FONT='Verdana','sans-serif']
[FONT='Verdana','sans-serif']Code Sample From Default.aspx.vb:
[FONT='Verdana','sans-serif']
ProtectedSub GridView24_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GridView24.SelectedIndexChanged
If reportPanel.Visible = FalseThen
reportPanel.Visible = True
EndIf
EndSub

Your assistance is greatly appreciated.

William Aldrich-Thorpe

wthorpe
Newbie Poster
2 posts since Jun 2006
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You