Solved

Wildcard in dataset filter query

Posted on 2013-01-08
5
291 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Visual Studio editor? 1 38
Close word object 13 38
Entity Framework 7 30
.NET tools for adding thread safety to a web app? 3 13
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Edureka is one of the fastest growing and most effective online learning sites.  We are here to help you succeed.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now