If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Microsoft SQL Server > Selecting multiple values from a INT Field.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-27-08, 07:54
MrRalphMan MrRalphMan is offline
Registered User
 
Join Date: Jul 2007
Posts: 50
Question Selecting multiple values from a INT Field.

Hi,

If I have a field which is an INT and holds different status codes, I would like to be able to seach this field for multiple values.

IE

Status codes =

New = 1
Open = 2
Resolved = 3
Closed = 4

If I want to search for all items that are New or Closed, I would like to be able to provide 1 and 4 at search time.

I was thinking about using the Binary process. ie.

New = 1
Open = 2
Resolved = 4
Closed = 8

And be able to provide a amalgamation on the search criteral (9 in this instance) and for it to be able to work out 1 and 8 and in this critearia and if these values are in the field.

Any ideas or am I going about it all the wrong way?

Cheers,

Paul.
Reply With Quote
  #2 (permalink)  
Old 10-27-08, 09:10
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
WHERE status IN (1,4)
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 10-28-08, 08:33
MrRalphMan MrRalphMan is offline
Registered User
 
Join Date: Jul 2007
Posts: 50
Hi,

Yeah I think I was trying to make things more difficult for myself, but on thing I am not 100% sure about is passing the param for the IN statement to the proc.

Do I just use a string?
Reply With Quote
  #4 (permalink)  
Old 10-28-08, 08:48
Thrasymachus Thrasymachus is offline
SQL Server Street Fighter
 
Join Date: Nov 2004
Location: Down The Rabbit Hole
Posts: 7,979
typically I pass in a string that I parse into a table variable and then I join to my table variable and that typically takes care of the filtering I am trying accomplish.
__________________
software development is where smart people go to waste their lives
Reply With Quote
  #5 (permalink)  
Old 10-28-08, 08:57
MrRalphMan MrRalphMan is offline
Registered User
 
Join Date: Jul 2007
Posts: 50
I'm thinking of going with an XML param and an IF statement in the where clause.
Reply With Quote
  #6 (permalink)  
Old 10-28-08, 09:16
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Here's another in-elegant option
Code:
DECLARE @values varchar(20)
    SET @values = '|1|4|'

SELECT *
FROM   my_table
WHERE  CharIndex('|' + Convert(varchar, status) + '|', @values) > 0
Roll on 2008's ability to pass table variables to procedures!
__________________
George
Twitter | Blog
Reply With Quote
  #7 (permalink)  
Old 10-28-08, 09:20
pootle flump pootle flump is offline
King of Understatement
 
Join Date: Feb 2004
Location: One Flump in One Place
Posts: 14,905
Quote:
Originally Posted by georgev
Roll on 2008's ability to pass table variables to procedures!
I'll be interested to see Pat's thoughts on this.
__________________
Testimonial:
Quote:
pootle flump
ur codings are working excelent.
Reply With Quote
  #8 (permalink)  
Old 10-28-08, 09:34
Thrasymachus Thrasymachus is offline
SQL Server Street Fighter
 
Join Date: Nov 2004
Location: Down The Rabbit Hole
Posts: 7,979
my feeble little brain is having a hard time fathoming an objection to table parameters. maybe we can just keep bumoing this thread until the irishman weighs in.
__________________
software development is where smart people go to waste their lives
Reply With Quote
  #9 (permalink)  
Old 10-28-08, 09:38
pootle flump pootle flump is offline
King of Understatement
 
Join Date: Feb 2004
Location: One Flump in One Place
Posts: 14,905
His objection to deliminted strings is to do with relational contracts. I don't know if he will consider table parameters non-relational, or if he now considers the contract to have changed.
__________________
Testimonial:
Quote:
pootle flump
ur codings are working excelent.
Reply With Quote
  #10 (permalink)  
Old 10-28-08, 09:55
MrRalphMan MrRalphMan is offline
Registered User
 
Join Date: Jul 2007
Posts: 50
Bearing in mind my stored proc will have around 10 fields that can me selected using multiple values ala a IN clause, what is going to be the easiest way to do this?
Reply With Quote
  #11 (permalink)  
Old 10-28-08, 10:04
pootle flump pootle flump is offline
King of Understatement
 
Join Date: Feb 2004
Location: One Flump in One Place
Posts: 14,905
Quickest and dirtiest is George's idea.
Most thorough and elegant is XML.
Best compromise is Sean's idea.
__________________
Testimonial:
Quote:
pootle flump
ur codings are working excelent.
Reply With Quote
  #12 (permalink)  
Old 10-28-08, 10:14
MrRalphMan MrRalphMan is offline
Registered User
 
Join Date: Jul 2007
Posts: 50
With the XML, I can do a join on the XML input, but these params are optional, so if it's NULL how do I say select all?
Reply With Quote
  #13 (permalink)  
Old 10-28-08, 10:17
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Quote:
Originally Posted by pootle flump
Quickest and dirtiest is George's idea.
Score! +1 point
__________________
George
Twitter | Blog
Reply With Quote
  #14 (permalink)  
Old 10-28-08, 10:19
MrRalphMan MrRalphMan is offline
Registered User
 
Join Date: Jul 2007
Posts: 50
Not always the best method, but we are talking DB's here..

This works.

AND IM_CurrentPriority IN (SELECT ParamValues.ID.value('.','VARCHAR(20)')FROM @Priority.nodes('/priority/privalue') as ParamValues(ID))

But attempting to cater for a NULL value doesn't
AND (@Priority IS NULL or (IM_CurrentPriority IN (SELECT ParamValues.ID.value('.','VARCHAR(20)')FROM @Priority.nodes('/priority/privalue') as ParamValues(ID)))

Last edited by MrRalphMan; 10-28-08 at 10:44.
Reply With Quote
  #15 (permalink)  
Old 10-28-08, 11:37
Peso Peso is offline
Registered User
 
Join Date: Apr 2007
Posts: 183
Do the binary calculation on client side and pass value to stored procedure?

Code:
-- Prepare sample data
DECLARE	@Sample TABLE
	(
		data VARCHAR(20),
		binVal INT
	)

INSERT	@Sample
SELECT	'Peso', 15 UNION ALL
SELECT	'dbForums', 9 UNION ALL
SELECT	'SQL Server', 5 UNION ALL
SELECT	'Microsoft', 3

-- Prepare user supplied binary value
DECLARE	@param INT

-- Calculate the binary value for bit 1 and 4 set at client side. Value is 9.
SET	@param = POWER(2, 1 - 1) + POWER(2, 4 - 1)

-- Search the table for exact match
SELECT	data
FROM	@Sample
WHERE	binVal & @param = @param

-- Search the table for match on any of the given bits
SELECT	data
FROM	@Sample
WHERE	binVal & @param > 0
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On