How to grab rows matching a user-defined search string, in SQL Server?

F

Fool

I am a writing a small database app using VB.net and SQL server (MSDE
for now).

When the user types a string into a text box, the program needs to
search a description column in a table on the server, and return all
rows from that table that match the text string. So for example, if
the user types "big", the returned table (in a datagrid) might look
like this:

Item Description (searched column) Other Data Column

Big Item 0028
Biggest Item 8373
That big huge item 9238
Abigail's Item 8362

I am only familiar with basic SQL commands, and am unsure how best to
return all rows that match the search parameters. I know how to return
rows with an *exact* value (e.g., rows that simply have the word 'big'
in the description column), but not how to use "fuzzy" matching to
grab all rows whose searched column contains some form of the search
text somewhere in the column.

The two main possibilities I see are to write a SQL query that somehow
incorporates that "fuzzy" matching, or (less preferable) to just grab
the entire table, slap it in a dataset, and then use visual basic
commands to search the dataset for the rows I want. Then I would put
the matching rows in a second dataset (or a different table at least)
and bind my datagrid to the second, smaller dataset. The downside is
that it would have to grab the whole darn table off the server, every
time, instead of just returning a specific set of rows.

I am using the excellent Murach book on ado.net, which has helped a
lot, but it doesn't show any instances of how to return records based
on these sorts of "fuzzy" queries. The examples usually bind a
particular column to a drop-down list or some such, and the user
changes the select command simply by picking items from the drop-down.

Can someone point me in the right direction? Any help would be much
appreciated.


-fool
 
V

Val Mazur \(MVP\)

Hi,

You need to use LIKE clause with the wildcards. Your query in this case
would look like

SELECT ..... FROM MyTable WHERE Description LIKE '%big%'
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top