473,657 Members | 2,624 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Separate an And/Or parameter

I want a user to be able to search for names in a table of clients
using a single parameter, but be eable to use "and" or "or" IN the
parameter.
Like this:

@NameSearch nvarchar(100)

AS

DECLARE @PartA nvarchar(100), @PartB nvarchar(100)

CASE When @nameSearch Like '%' + ' OR ' + '%'

SELECT @PartA =(how do I get what's to the left of "OR" or "AND")
SELECT @PartB (how do I get what's to the right of "OR" or "AND")

SELECT
c.UniqueID,
c.LastName
FROM
dbo.tblClients c
WHERE
c.LastName = @PartA
OR
c.LastName = @PartB

CASE ELSE

SELECT
c.UniqueID,
c.LastName
FROM
dbo.tblClients c
WHERE
c.LastName = @NameSearch

Jul 23 '05 #1
14 1494

"laurenq uantrell" <la************ *@hotmail.com> wrote in message
news:11******** **************@ l41g2000cwc.goo glegroups.com.. .
I want a user to be able to search for names in a table of clients
using a single parameter, but be eable to use "and" or "or" IN the
parameter.
Like this:

@NameSearch nvarchar(100)

AS

DECLARE @PartA nvarchar(100), @PartB nvarchar(100)

CASE When @nameSearch Like '%' + ' OR ' + '%'

SELECT @PartA =(how do I get what's to the left of "OR" or "AND")
SELECT @PartB (how do I get what's to the right of "OR" or "AND")

SELECT
c.UniqueID,
c.LastName
FROM
dbo.tblClients c
WHERE
c.LastName = @PartA
OR
c.LastName = @PartB

CASE ELSE

SELECT
c.UniqueID,
c.LastName
FROM
dbo.tblClients c
WHERE
c.LastName = @NameSearch


You could probably use a combination of string parsing and dynamic SQL to do
this, but for more complicated cases it would quickly become very difficult
to maintain and secure. Or you could pass in a list of names, and use a
technique like this:
Jul 23 '05 #2
<snip>

You could probably use a combination of string parsing and dynamic SQL to
do this, but for more complicated cases it would quickly become very
difficult to maintain and secure. Or you could pass the names as a list,
and use a technique like this:


Oops - hit Send too quickly...

.... a technique like this:

http://www.sommarskog.se/arrays-in-sql.html

This might also give you some ideas:

http://www.sommarskog.se/dyn-search.html

You might want to consider looking at a reporting tool - if your
requirements become more complex, it will be a lot easier than trying to
write and maintain stored procedures to cover every case that your users
would like to see. MSSQL Reporting Services is free if you have an MSSQL
license, or there are other tools such as Crystal/Business Objects
available.

Simon
Jul 23 '05 #3
laurenq uantrell (la************ *@hotmail.com) writes:
I want a user to be able to search for names in a table of clients
using a single parameter, but be eable to use "and" or "or" IN the
parameter.
Like this:

@NameSearch nvarchar(100)

AS

DECLARE @PartA nvarchar(100), @PartB nvarchar(100)

CASE When @nameSearch Like '%' + ' OR ' + '%'

SELECT @PartA =(how do I get what's to the left of "OR" or "AND")
SELECT @PartB (how do I get what's to the right of "OR" or "AND")


I would suggest that it quickly gets messy if you are to parse this in
T-SQL. I don't know the exact context for your problem, but I would
look into parsing these lists in the client, and then feed the stored
procedure somehow. For a more general input, XML could do. Then again,
you will still have to build the SQL dynamically, as every OR or
AND would add one more term to the WHERE clause. If you restrict
the number of OR and AND you support, you can use a couple of
fixed parameters.

--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #4
Erland, yes, parsing this in the client seems the best solution. In
this way I can limit the number of "AND" or "OR" links to say, three,
then provide three parameters that can be ignored if blank. Thanks to
you and Simon (Thanks AGAIN, I might add!)
lq

Jul 23 '05 #5
Just thinking further on this, not to get too complicated, how would
one go about creating a stored procedure where the client side looked
like this:

ClientName LIKE "Smith"
AND/OR
City IS "London"
AND/OR
Pet LIKE "Spot"
etc...

Where the user enters the quoted criteria and then selects AND or OR
and LIKE or IS

Jul 23 '05 #6
Here is an old posting of mine that handles building queries with a
canonical disjunctive form representation in a table The origianl
problem was matching people to job requirements.

========
I think what he is after is the ability to load tables with criteria
and not have to use dynamic SQL:

skill = Java AND (skill = Perl OR skill = PHP)

becomes the canonical disjunctive form:

(Java AND Perl) OR (Java AND PHP)

which we load into this table:

CREATE TABLE Query
(and_grp INTEGER NOT NULL,
skill CHAR(4) NOT NULL,
PRIMARY KEY (and_grp, skill));

INSERT INTO Query VALUES (1, 'Java');
INSERT INTO Query VALUES (1, 'Perl');
INSERT INTO Query VALUES (2, 'Java');
INSERT INTO Query VALUES (2, 'PHP');

Assume we have a table of job candidates:

CREATE TABLE Candidates
(candidate_name CHAR(15) NOT NULL,
skill CHAR(4) NOT NULL,
PRIMARY KEY (candidate_name , skill));

INSERT INTO Candidates VALUES ('John', 'Java'); --winner
INSERT INTO Candidates VALUES ('John', 'Perl');
INSERT INTO Candidates VALUES ('Mary', 'Java'); --winner
INSERT INTO Candidates VALUES ('Mary', 'PHP');
INSERT INTO Candidates VALUES ('Larry', 'Perl'); --winner
INSERT INTO Candidates VALUES ('Larry', 'PHP');
INSERT INTO Candidates VALUES ('Moe', 'Perl'); --winner
INSERT INTO Candidates VALUES ('Moe', 'PHP');
INSERT INTO Candidates VALUES ('Moe', 'Java');
INSERT INTO Candidates VALUES ('Celko', 'Java'); -- loser
INSERT INTO Candidates VALUES ('Celko', 'Algol');
INSERT INTO Candidates VALUES ('Smith', 'APL'); -- loser
INSERT INTO Candidates VALUES ('Smith', 'Algol');

The query is simple now:

SELECT DISTINCT C1.candidate_na me
FROM Candidates AS C1, Query AS Q1
WHERE C1.skill = Q1.skill
GROUP BY Q1.and_grp, C1.candidate_na me
HAVING COUNT(C1.skill)
= (SELECT COUNT(*)
FROM Query AS Q2
WHERE Q1.and_grp = Q2.and_grp);

You can retain the COUNT() information to rank candidates. For example
Moe meets both qualifications, while other candidates meet only one of
the two. if you can think in sets, you can avoid dynamic SQL and
procedural 99% of the time.

Jul 23 '05 #7
--CELKO-- (jc*******@eart hlink.net) writes:
INSERT INTO Candidates VALUES ('Larry', 'Perl'); --winner


Of course Larry is a winner when it comes to Perl!

--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #8
laurenq uantrell (la************ *@hotmail.com) writes:
Just thinking further on this, not to get too complicated, how would
one go about creating a stored procedure where the client side looked
like this:

ClientName LIKE "Smith"
AND/OR
City IS "London"
AND/OR
Pet LIKE "Spot"
etc...

Where the user enters the quoted criteria and then selects AND or OR
and LIKE or IS


I rather not do it at all!

If was meant to be general, I would probably see no way out but to
use dynamic SQL.

From your first example, I got the impression that all search
terms were for the same column, in which case a static solution
would be easier. I don't any examples, though, because I don't
know your business requierments well enough.

In any case, I have a longer article on the topic, that might
give you inspiration: http://www.sommarskog.se/dyn-search.html.

--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #9
I think he padded his resume ...Like that Celko giuy claiming 104 years
of SQL experience.

Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1965
by: Burt Lewis | last post by:
Hi, I have 2 xsl style sheets calling separate rss feeds and I use ASP to display. Problem is that the 1st xsl takes on the style of the 2nd xsl even though they are different formats. I think I need to clear from memory but not sure how. This is the code and sample outputs. They work great on their own but when I call them both they display the same formats. Thanks for any help.
3
1868
by: Roberto Becerril | last post by:
Hi forum, i'm a little new in javascript and maybe you can help me. I have an html form and an icon, if i click on the icon, a new pop-up window is open and shows a list of numbers with a structure like this : x.xx.xxx.xxxx. Now, this numbers are between an <a href> tag and if i clic on one of this, i call a javascript function, the idea is to put each one number separate for the "." in a textbox, so i did it before but putting it
2
1823
by: Mike Hennessy | last post by:
I'm looking for people's opinions and feedback regarding the design of the application tier, and how to best logically separate out the Data Access from the Business Object's. Per the Microsoft prescriptive architecture documents, they recommend creating a completely separate logical Data Access Tier of components. Then creating a separate tier of Business Objects that consume them. My first question is...what does this actually buy you...
28
4313
by: kfrost | last post by:
I know this is probably simple but I have a C# form and the class for the form is called sbaSynch. I have a textbox name txtServerName. I'm creating a class to manipulate XML functions so I added a class to project and it's named XmlApi(). In the XmlAPI() class I have simple code as following XmlAPI() { string str = "Some Text";
10
2335
by: Zoe Hart | last post by:
I have a wsdl file that I received from a third party and I'm using wsdl.exe (.NET 2.0) to import it and generate a proxy class. I've actually got a proxy class that works, but I'm trying to understand why wsdl.exe did what it did and whether or not the complexity it's added to the interface is necessary or avoidable. The wsdl file specifies a Boolean parameter pAggregatePurses for one of the web service methods. In the proxy class...
3
4558
by: LurfysMa | last post by:
I would like to hear opinions on the tradeoffs of putting the tables, forms, and queries for several related datasets in separate databases vs one combined database. I am working on an application that will have a number of "subjects". The subjects have a number of commonalities, but are not identical. I am hoping that I will discover that the subjects fall into a few "types" and a common database structure can be used for each type. ...
5
2359
by: =?iso-8859-1?q?Erik_Wikstr=F6m?= | last post by:
The following code works: #include <iostream> template<class T> struct Test { T t; template<class T, template<class U = Tclass V> Test<T>& foo(V<T>& f)
7
1702
by: forest demon | last post by:
all i want is to do is to pass a form reference to a separate class and be able to manipulate properties/components/controls of said form. this should be as simple as passing a TextBox, Container object or something similar, to do the same, but no. what am i missing, besides my mind? thanks folks...
13
19940
by: dizzydangler | last post by:
Just a quick question...I'm running an MS Access 2007 db that tracks appointments in a single table. Date and time are entered as separate fields in short date (mm/dd/yyyy) and short time (hh:mm) format. I've been running queries by date alone, which has been working, but there are times when I have to run queries based on times as well. (e.g. return all apointments that occurred after 1:00 PM on 12/15/2009 and before 1:00 PM on 12/22/2009). ...
0
8427
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8850
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8626
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7355
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4334
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1975
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1737
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.