?
Solved

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

Posted on 2014-09-07
9
Medium Priority
?
347 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1800 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 37

Assisted Solution

by:Neil Russell
Neil Russell earned 200 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
 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses
Course of the Month12 days, 15 hours left to enroll

777 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