0

I wants to separate string by comma. Actually I wants to filter in WHERE Clause like

CREATE PROC spGetRecords
@class varchar(50)
AS
BEGIN
    SELECT *
    FROM SampleTable
    WHERE class in (@class)  --in Database class is an integer
END

So, I want to pass the parameter when execute query like below

spGetRecords @class = '12,22,45,66'

I think it's not possible to pass multiple value in single parameter in my case.

So, If I remove separate the string by ',' then I can run my query in while loop that will bring the record correct?

So, How can I separate the string by comma

5
  • What RDBMS and version are you using. Commented Jul 15, 2017 at 10:23
  • Use dynamic SQL: exec 'select ...' Commented Jul 15, 2017 at 10:25
  • @Igor Microsoft sql server 2012 11.0.2100.60 Commented Jul 15, 2017 at 10:27
  • @juergend what it will make sense? Commented Jul 15, 2017 at 10:28
  • If you're using SQL Server 2008 or newer, you should use table-valued parameter which is the proper and most efficient way of doing this Commented Jul 15, 2017 at 13:51

3 Answers 3

3

You can try splitting using xml path as below:

Declare @delimiter nvarchar(max) = ','
Declare @str nvarchar(max) = '12,22,45,66'

Declare @xml as xml
        Select @xml = CAST('<x>' + REPLACE((SELECT REPLACE(@str,@delimiter,'$$$SSText$$$') AS [*] FOR XML PATH('')),'$$$SSText$$$','</x><x>')+ '</x>' AS XML) 

--select @xml
Select y.value(N'text()[1]', N'nvarchar(MAX)') as value 
FROM @xml.nodes(N'x') as x(y) 

If you are in SQL Server >= 2016 you can use STRING_SPLIT() as below:

Select * from string_split('12,22,45,66',',')
Sign up to request clarification or add additional context in comments.

2 Comments

your answer is correct. But before I give correct, can you give explanation about third and 4th line?. thanks
Try selecting individual lines you will understand, first step converting into xml using xml path, while create nodes i am changing comma/delimiter as separate node and second line traversing thru all nodes
1

Use dynamic SQL

exec 'SELECT * FROM SampleTable WHERE class in (' + @class + ')'

which will patch the strings together and then execute it like this:

SELECT * FROM SampleTable WHERE class in (12,22,45,66)

3 Comments

class is an integer
it's a multilne query, not single line. For giving an example only I mentioned sample table.. But actually it is little bit lengthy query
So? Just put it in a string like in my example.
0

Here is a good solution - Converting String List into Int List in SQL . Idea here is to send in an int list to a stored procedure as an XML parameter.

Here is another quick but dirty solution - t-sql Convert comma separated string into int, without using user created function . Here the passed in value is used within a dynamic sql statement.

More ideas - http://sql-articles.com/scripts/function-to-split-comma-separated-string-to-integer/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.