Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


select where searchvalues are in list


select where searchvalues are in list

Author
Message
Mike Saunders NL
Mike Saunders NL
Grasshopper
Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)

Group: General Forum Members
Points: 23 Visits: 92
Hello all,

I am trying to select row from a table where the column contains values from a list. I tried the following but that query gave me an error (Conversion failed when converting the varchar value '1 OR [AttributeID] = 3 OR [AttributeID] = 6 OR [AttributeID] = 2808' to data type int.):


DECLARE @strSearch VARCHAR(100) = '1,3,6,2808'

SELECT *
FROM [AttributeList]
WHERE [AttributeID] = REPLACE(@strSearch, ',', ' OR [AttributeID] = ')



The [AttributeID] is an integer type column and i use SQL Server 2012.

Can someone help me with this, and is this the most efficient way to search?

Best christmas wishes
Mike
GilaMonster
GilaMonster
SSC-Forever
SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)

Group: General Forum Members
Points: 47427 Visits: 44405
Dynamic SQL (watch for SQL injection) or a string splitter function. I recommend the latter. Search this site for 'Delimited8kSplit'

Oh, and as for why your attempt failed, that equates to this:

SELECT *
FROM [AttributeList]
WHERE [AttributeID] = '1 OR [AttributeID] = 3 OR [AttributeID] = 6 OR [AttributeID] = 2808';

ie where the attribute is equal to that long string, which I'm sure you'll agree isn't going to do what you want


Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


Mike Saunders NL
Mike Saunders NL
Grasshopper
Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)

Group: General Forum Members
Points: 23 Visits: 92
Hi GilaMonster en Celko,

Thanks for your reactions. With help of your tips i've solved my puzzle.
@Celko: Thanks again for your lessons in netiquette, still learning......:-)

Mike
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45450 Visits: 39946
GilaMonster (12/23/2012)
Dynamic SQL (watch for SQL injection) or a string splitter function. I recommend the latter. Search this site for 'Delimited8kSplit'


Here's the link for the DelimitedSplit8K splitter.
http://www.sqlservercentral.com/articles/Tally+Table/72993/

If you need something more sophisticated or need to handle something longer than 8K bytes, I strongly recommend a CLR.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search