Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
allan8964
Posting Yak Master
249 Posts |
Posted - 2014-03-20 : 16:16:12
|
Hi there,I want to bring a string parameter to where clause like this:select .. from ...Where Code IN (@Code)When @Code is 'ABC' it works but when @code is 'ABC, XYZ' it won't work. I know for IN statement we should use IN ('ABC', 'XYZ') and I tried make @code = '''ABC'', ''XYZ''' still not working. I tried Print '''ABC'', ''XYZ''' I got 'ABC', 'XYZ' but why it does not work in @Code?Thanks in advance. |
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
|
MuralikrishnaVeera
Posting Yak Master
129 Posts |
Posted - 2014-03-21 : 00:47:13
|
It is not possible to use 'IN Clause' in that FashionThe below answers your Question....DECLARE @Id VARCHAR(100) = '25,107'Select SUBSTRING(@ID ,PATINDEX('%,%',@ID)+1 ,(LEN(@ID))) AS FirstNumberSElect SUBSTRING(@ID ,0,PATINDEX('%,%',@ID)) AS SecondNumberNow you are able to use Those 'First Number' and 'Second Number' in your where clause.........Murali Krishna |
|
|
VeeranjaneyuluAnnapureddy
Posting Yak Master
169 Posts |
Posted - 2014-03-21 : 01:03:14
|
quote: Originally posted by MuralikrishnaVeera It is not possible to use 'IN Clause' in that FashionThe below answers your Question....DECLARE @Id VARCHAR(100) = '25,107'Select SUBSTRING(@ID ,PATINDEX('%,%',@ID)+1 ,(LEN(@ID))) AS FirstNumberSElect SUBSTRING(@ID ,0,PATINDEX('%,%',@ID)) AS SecondNumberNow you are able to use Those 'First Number' and 'Second Number' in your where clause.........Murali Krishna
It can usefull for passing two values in string parameter not for multi values in string parameter.....DECLARE @Id VARCHAR(100) = '1235,346,136,1643,8513'Select SUBSTRING(@ID ,PATINDEX('%,%',@ID)+1 ,(LEN(@ID))) AS FirstNumberSElect SUBSTRING(@ID ,0,PATINDEX('%,%',@ID)) AS SecondNumberVeera |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2014-03-21 : 11:32:02
|
I don't know if it is worth mentioning or not that Veera's solution cannot make use of an index seek, which may or may not be important in your case. |
|
|
|
|
|
|
|