Solved

VB.Net - SQL Query with "In" list and "Like"

Posted on 2014-09-07
9
286 Views
Last Modified: 2014-09-12
Good Day Experts!

I have a little task here that I have not come across so far.  I need to build a query that contains and "In" range as well as a "like" characteristic.

Select * from Customer where Account in ('Test', 'Test1, 'Test2')

But the Account field has other data in it in addition to the actual Account value.

So, I need to be able to a like with the above query.  Is  this possible?

Thanks,
jimbo99999
0
Comment
Question by:Jimbo99999
  • 4
  • 3
  • 2
9 Comments
 
LVL 1

Expert Comment

by:Subramani N
ID: 40308566
You can build the query like this.
SELECT * FROM Customer WHERE Account IN ('Test', 'Test1, 'Test2') OR Account LIKE '%Test%' OR Account LIKE '%Test1%' OR Account LIKE '%Test2%'

Open in new window

This query will return the rows having Account Test/Test1/Test2 or If the Account has any of the words Test, Test1, Test2 in it. Any part of the value. If you want check for the Starting of the value, then use "Test%".
0
 

Author Comment

by:Jimbo99999
ID: 40308582
Thanks for responding.  I was building the list of Account from a multi-select listView making the "In" part between the parenthesis.  But then I realized that the Account field has other data as well as the value I am looking for...that is all the time and will never have just the Account value.

Is there anyway to say in ('%Test%', '%Test1%', '%Test2%') with a like?  Or I can build the string like you suggested with the Or's...which I didn't think of!

Thanks,
jimbo99999
0
 
LVL 1

Accepted Solution

by:
Subramani N earned 450 total points
ID: 40308590
You can do one thing. In your dotnet code, Loop through your list and Concatenate all your strings(selected list items) as one String and add "Account and '%%'" in iteration. Finally, add the built condition to your query and execute it in the server.

This is the only way. I'm sorry. No. There is no way you could do it. SQL Server does not support this. Adding multiple ORs is the only way.
0
 
LVL 37

Assisted Solution

by:Neil Russell
Neil Russell earned 50 total points
ID: 40308697
OK, follow along..... Works like a charm to do exactly what you ask....


1) CREATE TEMPORARY TABLE Patterns( Pattern VARCHAR(20) );
creates a temp table to hold your patterns to look for.

2) INSERT INTO Patterns VALUES ('TEST1%'), ('TEST2%'),('ANOTHERTEST%');
this adds the set of patterns you want to match into the TEMP table.

3) SELECT a.* FROM CUSTOMER a JOIN Patterns p ON (a.account LIKE p.Pattern);
Now do a SELECT that joins on a LIKE
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Expert Comment

by:Subramani N
ID: 40308701
Neilsr...Jimbo is querying the database directly from his vb code and not from a procedure.
0
 
LVL 37

Expert Comment

by:Neil Russell
ID: 40308711
I don't see the difference.  Create a stored procedure on the database that accepts the list of patterns and returns the results using that code.  End result is one request to the database engine giving exactly the results required.

UNLESS this is a database that you can not get any form of administrative access to ?
0
 
LVL 1

Expert Comment

by:Subramani N
ID: 40308735
As per Jimbo's statement, the Patterns are from "multi-select listView" that the user chosen from the win/web app. This has to be passed to the database in order get the procedure work.

In case if he chooses to go for a procedure, then he has to create another table in the database along with the stored procedure like the below example.


CREATE TABLE dbo.AccontTable --Actual table
(
    AccountNo	VARCHAR(10),
    AccountName VARCHAR(55),
    AccountType	VARCHAR(10)
)

CREATE TYPE dbo.AccountPattern AS TABLE --Pattern Type
(
    Pattern	VARCHAR(10)
)

Open in new window

Create the Stored Procedure that has the table variable as the parameter
CREATE PROCEDURE dbo.AccountPatternMatch
(
    @PatternVar dbo.AccountPattern READONLY
)
AS
BEGIN
    
    SELECT AccountNo, AccountName FROM dbo.AccontTable account
    JOIN AccountPattern pattern ON (account.AccountType LIKE pattern.Pattern)
    WHERE AccountType IN (SELECT Pattern FROM @PatternVar)
        
END
GO

Open in new window

And...In the VB code:

1. Populate the "AccountPattern" table from the multi-select list view

2. Call the Stored procedure
0
 
LVL 37

Expert Comment

by:Neil Russell
ID: 40308741
@Subramani N

That is exactly what i said he could do yes. Except that you have introduced a WHERE clause on fields that were not discussed.  The simple 1,2,3 steps I provided above answer the question asked as one possible solution.
0
 

Author Closing Comment

by:Jimbo99999
ID: 40319165
Thank you Subramani N as I used your solution.  

Have a a good day and weekend everyone,
jimbo99999
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

759 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

24 Experts available now in Live!

Get 1:1 Help Now