Solved

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

Posted on 2014-09-07
9
302 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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
 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

785 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