Important alert: (current site time 7/15/2013 8:02:02 PM EDT)
 

article

Logical Operator "&" instead of using IN

Email
Submitted on: 12/19/2005 6:36:42 PM
By: Bornancini Ignacio  
Level: Intermediate
User Rating: By 2 Users
Compatibility: SQL Server 2000, SQL Server 7.0
Views: 9567
author picture
(About the author)
 
     How to search with only one parameter several values of a field in a table

 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
				let us suppose that we have a Web Page where we can look for customers by countryes. The web page gives as the chance to choose several countryes at the same time. We have multiple checkboxes with the name of the options (not only one).
let's create the table to star this example
CREATE TABLE [customers] ( [idreg] [int] IDENTITY (1, 1) NOT NULL , [countryid] [int] NULL , [data] [char] (10) ) ON [PRIMARY] GO Now we can insert some data
INSERT INTO customers(countryid, data) values
(2,'customer1')
INSERT INTO customers(countryid, data) values
(8,'customer2')
INSERT INTO customers(countryid, data) values
(8,'customer3')
INSERT INTO customers(countryid, data) values
(16,'customer4')
INSERT INTO customers(countryid, data) values
(16,'customer5')
INSERT INTO customers(countryid, data) values
(32,'customer6')
INSERT INTO customers(countryid, data) values
(32,'customer7')
GO
We need to obtain for example the customers of
the country 8 and 32 (8+32=40) SELECT *
FROM customers
WHERE (countryid & 40 )>0
...and the customers from the contryes 16,32 and 2 (16+32+2=50)
SELECT *
FROM customers
WHERE (countryid & 50 )>0
in other way we used to do something like this
SELECT *
FROM customers
WHERE countryid IN (16,32,2)
The advantage of using "&" operator is that we
only have one parameter to look for
multiple values. We only have to sum the values of the countryid. The county id must be values like (2,4,8,32,64,128,256...) I hope you like that...and sorry for my english!!!


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

12/20/2005 8:31:10 AMmid.net

really nice code!!!

I like it
(If this comment was disrespectful, please report it.)

 
1/19/2006 7:27:11 AMSyed Muhammad Fahad

The Code is not generalized ... It works only when we have even numbered values ... Will fail for Odd Values ...
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.