|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Friday, March 22, 2013 4:22 AM
Points: 131,
Visits: 184
|
|
How about adding delimiters? Avoiding ardy and ocky.
DECLARE @inputValue NVARCHAR(20) DECLARE @student TABLE (Id INT PRIMARY KEY IDENTITY(1,1), StudentName NVARCHAR(50), StudentResult INT)
INSERT INTO @student VALUES( 'Hardy', 100), ('Rocky', 98), ('Panky', 99), ('ardy', 97), ('ocky', 96)
--Select 1 SET @inputValue = 'Hardy' SELECT * FROM @student WHERE StudentName IN (@inputValue)
--Select 2 SET @inputValue = 'Hardy,Rocky' SELECT * FROM @student WHERE CHARINDEX(','+StudentName+',', ','+@inputValue+',') > 0
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Yesterday @ 7:11 AM
Points: 877,
Visits: 1,159
|
|
@sfellner : If you modify Select 2 as follows:
--Select 2 SET @inputValue = 'Hardy, Rocky, ardy' SELECT * FROM @student WHERE CHARINDEX(','+StudentName+',', ','+@inputValue+',') > 0
It will only return 'Hardy'.
Thanks
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Friday, March 22, 2013 4:22 AM
Points: 131,
Visits: 184
|
|
it is because in the sample the delimiter became now comma and space. both possibilities will work:
SET @inputValue = 'Hardy, Rocky, ardy' SELECT * FROM @student WHERE CHARINDEX(', '+StudentName+',', ', '+@inputValue+',') > 0
SET @inputValue = 'Hardy,Rocky,ardy' SELECT * FROM @student WHERE CHARINDEX(','+StudentName+',', ','+@inputValue+',') > 0 HTH Stefan
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Tuesday, March 19, 2013 7:05 PM
Points: 110,
Visits: 439
|
|
the question should include SQL versions. the following does not work prior to 2008.
INSERT INTO @student VALUES( 'Hardy', 100), ('Rocky', 98), ('Panky', 99)
|
|
|
|
|
Say Hey Kid
      
Group: General Forum Members
Last Login: Monday, March 11, 2013 4:12 AM
Points: 664,
Visits: 138
|
|
I think this part is not correct INSERT INTO @student VALUES( 'Hardy', 100), ('Rocky', 98), ('Panky', 99)
it will throw error.
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Yesterday @ 7:11 AM
Points: 877,
Visits: 1,159
|
|
amit_adarsh (1/14/2011) I think this part is not correct INSERT INTO @student VALUES( 'Hardy', 100), ('Rocky', 98), ('Panky', 99)
it will throw error.
This is SQL Server 2008 syntax. If you want to execute it in SQL 2005 then use the following:
INSERT INTO @student VALUES ( 'Hardy', 100) INSERT INTO @student VALUES ('Rocky', 98) INSERT INTO @student VALUES ('Panky', 99)
Thanks
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Wednesday, February 06, 2013 11:51 PM
Points: 1,263,
Visits: 1,079
|
|
Thanks for the question.
It may sound stupid to the more experienced users, but I don't understand why the following code is not working (using SQL Server 2005; pasted just my amended query--you'll need the original one, too):
--Select 3 SET @inputValue = '''Hardy''' + ', ' + '''Rocky''' select @inputValue -- added to view the output of the variable SELECT * FROM @student WHERE StudentName IN (@inputValue) -- not working
Because if you write out the query without the variable, it will return the expected 2 records.
SELECT * FROM @student WHERE StudentName IN ('Hardy', 'Rocky') -- working OK
Any hints / explanations are welcome.
Thanks in advance, Michael
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 12:02 PM
Points: 5,243,
Visits: 7,055
|
|
michael.kaufmann (1/18/2011)
I don't understand why the following code is not working (using SQL Server 2005; pasted just my amended query--you'll need the original one, too): --Select 3 SET @inputValue = '''Hardy''' + ', ' + '''Rocky''' select @inputValue -- added to view the output of the variable SELECT * FROM @student WHERE StudentName IN (@inputValue) -- not working
It is working, just not as you expect it. If you look at the code above, you'll see that the IN list consists of exactly one member - the variable @inputValue. SQL Server will not care or interpret the content of that variable, but simply search the @student table for rows with a StudentName that matches that value - so you get all rows returned for students whose name is equal to 'Hardy', 'Rocky' (and I hope for their sake that no student has been given that name!)
To get SQL Server to find both Hardy and Rocky, you need to supply two arguments, seperated by a comma. The comma has to be in the IN list, not in the contents of the variables.
--Select 3 SET @inputValue1 = 'Hardy'; SET @inputValue2 = 'Rocky'; --select @inputValue -- added to view the output of the variable SELECT * FROM @student WHERE StudentName IN (@inputValue1, @inputValue2);
Hugo Kornelis, SQL Server MVP Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Wednesday, February 06, 2013 11:51 PM
Points: 1,263,
Visits: 1,079
|
|
Hello Hugo,
as it's obvious, I haven't had too many chances using variables (except for variables carrying exactly one distinct value)--hence I was under the impression the contents of the variable is used. Thank you very much for your kind and precise explanation.
Regards, Michael
|
|
|
|