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

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
Jimbo99999Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Subramani NCommented:
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
Jimbo99999Author Commented:
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
Subramani NCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Neil RussellTechnical Development LeadCommented:
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
Subramani NCommented:
Neilsr...Jimbo is querying the database directly from his vb code and not from a procedure.
0
Neil RussellTechnical Development LeadCommented:
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
Subramani NCommented:
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
Neil RussellTechnical Development LeadCommented:
@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
Jimbo99999Author Commented:
Thank you Subramani N as I used your solution.  

Have a a good day and weekend everyone,
jimbo99999
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.