Solved

Wildcard in dataset filter query

Posted on 2013-01-08
5
293 Views
Last Modified: 2013-01-08
Hello

I'm adding a query with a filter to a dataset with the designer.

My query is:-

SELECT    Counter, SiteRef, ccount, SiteName, AddressLine1, AddressLine2, AddressLine3, Town, Postcode
FROM            SiteDetails
WHERE        (Postcode = @POSTCODETOFIND )

This works OK but I'm struggling to get the LIKE and wildcard option to work.

Please could you suggest the correct syntax.

Thanks
0
Comment
Question by:Petermcg001
  • 3
  • 2
5 Comments
 
LVL 12

Accepted Solution

by:
Jared_S earned 500 total points
ID: 38755549
If your passing the wildcard to sql as part of a variable, then it's pretty easy to handle.

If you're trying to add it to your variable within your sql statement it has to be handled a little differently.

ex.
 
This works:

declare @name as varchar(15)
set @name = 'smit%'
select * from staff where lastname like @name

This, or any variation of this, won't work:

declare @name as varchar(15)
set @name = 'smit'
select * from staff where lastname like (@name + '%')


So with your code you can do this:

SELECT    Counter, SiteRef, ccount, SiteName, AddressLine1, AddressLine2, AddressLine3, Town, Postcode
FROM            SiteDetails
WHERE        (Postcode = @POSTCODETOFIND ) OR   (Postcode LIKE @POSTCODETOFIND )

as long as you're setting the wildcard character in your variable prior to passing it to sql.
0
 
LVL 1

Author Comment

by:Petermcg001
ID: 38755620
Hello, thanks for looking at this.

With your help I've got it to work but I'm struggling to understand the following.

If I use:-

" Dim strFind As String
        strFind = Trim(txtPostcode.Text) + "%"

        Me.SiteDetailsTableAdapter.FillBy(Me.OilDistributionSystemDataSet.SiteDetails, strFind)"

it doesn't work.  However if I use:-

 Dim strFind As String
        strFind = Trim(txtPostcode.Text) + "%%%%%%"

        Me.SiteDetailsTableAdapter.FillBy(Me.OilDistributionSystemDataSet.SiteDetails, strFind)

it does work.  I thought the "%" should match any string.
0
 
LVL 12

Expert Comment

by:Jared_S
ID: 38755726
In SQL % is any string, but in VB.net % is any character.

Try using * instead.
0
 
LVL 1

Author Comment

by:Petermcg001
ID: 38755740
Thanks for clarifying that and your help.
0
 
LVL 12

Expert Comment

by:Jared_S
ID: 38755912
No problem - happy coding!
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question