Solved

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

Posted on 2014-09-07
9
289 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
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.

 
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

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
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…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

929 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

12 Experts available now in Live!

Get 1:1 Help Now