SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 string parameter with comma
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

allan8964
Posting Yak Master

249 Posts

Posted - 03/20/2014 :  16:16:12  Show Profile  Reply with Quote
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.

Edited by - allan8964 on 03/20/2014 16:37:19

Lamprey
Flowing Fount of Yak Knowledge

4614 Posts

Posted - 03/20/2014 :  16:47:49  Show Profile  Reply with Quote
You can't do that. In order to do what you are trying to need to use Dynamic SQL, split/parse string function or a "funky" LIKE/PATINDEX comparison.

What you should be using is called a TVP (table-valued parameter):
http://blog.sqlauthority.com/2008/08/31/sql-server-table-valued-parameters-in-sql-server-2008/
http://technet.microsoft.com/en-us/library/bb510489.aspx

Edited by - Lamprey on 03/20/2014 16:51:24
Go to Top of Page

MuralikrishnaVeera
Posting Yak Master

India
107 Posts

Posted - 03/21/2014 :  00:47:13  Show Profile  Reply with Quote
It is not possible to use 'IN Clause' in that Fashion

The below answers your Question....

DECLARE @Id VARCHAR(100) = '25,107'
Select SUBSTRING(@ID ,PATINDEX('%,%',@ID)+1 ,(LEN(@ID))) AS FirstNumber
SElect SUBSTRING(@ID ,0,PATINDEX('%,%',@ID)) AS SecondNumber

Now you are able to use Those 'First Number' and 'Second Number' in your where clause.........

Murali Krishna
Go to Top of Page

VeeranjaneyuluAnnapureddy
Posting Yak Master

India
169 Posts

Posted - 03/21/2014 :  01:03:14  Show Profile  Reply with Quote
quote:
Originally posted by MuralikrishnaVeera

It is not possible to use 'IN Clause' in that Fashion

The below answers your Question....

DECLARE @Id VARCHAR(100) = '25,107'
Select SUBSTRING(@ID ,PATINDEX('%,%',@ID)+1 ,(LEN(@ID))) AS FirstNumber
SElect SUBSTRING(@ID ,0,PATINDEX('%,%',@ID)) AS SecondNumber

Now 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 FirstNumber
SElect SUBSTRING(@ID ,0,PATINDEX('%,%',@ID)) AS SecondNumber

Veera
Go to Top of Page

Lamprey
Flowing Fount of Yak Knowledge

4614 Posts

Posted - 03/21/2014 :  11:32:02  Show Profile  Reply with Quote
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.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000