Please start any new threads on our new site at We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 split function
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Posting Yak Master

129 Posts

Posted - 04/24/2013 :  09:03:06  Show Profile  Reply with Quote
Hi all.

I am passing a comma delimited list into a SPROC which uses the IN clause.

I need to split this so the SPROC "works" with it.
I will be passing it in this format:

'item1, item2, item3'

I need to then do a split on the comma in the SPROC.

How can I create a split function in SQL Server so I can make this sproc work with the input parameter?


CREATE PROCEDURE [dbo].[GetControlSerialNumberDuplicates] (
@controlNumbers varchar(2000) -- comma delimited for multiple

SELECT ControlNumber,ControlNextCalDueDate, ControlStatus
FROM Control
WHERE (ControlNumber IN (@controlNumbers)) AND ControlRecDel = 0


James K
Flowing Fount of Yak Knowledge

3873 Posts

Posted - 04/24/2013 :  09:08:02  Show Profile  Reply with Quote
There are many spslit functions available online. One of the most efficient ones is Jeff Moden's function here: The function is in Figure 21 of that article.
Go to Top of Page

Very Important crosS Applying yaK Herder

52326 Posts

Posted - 04/24/2013 :  12:42:26  Show Profile  Reply with Quote

SQL Server MVP
Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3873 Posts

Posted - 04/24/2013 :  17:26:36  Show Profile  Reply with Quote
Originally posted by visakh16


SQL Server MVP

Just be careful if you have any of the XML special characters (such as &, < etc.) in your string - this can fail. You can work around those with a little bit of effort. Even if you do that, this would be much less efficient compared to Jeff's code.

On the plus side, Visakh's method does not require you to install yet another function on your server. However, there are some functions that I consider well-worth installing - one being Jeff's delimitedSplit8K, and another is Adam Machanic's sp_whoisactive (which of course, is not a string splitter; it is completely something else).
Go to Top of Page
  Previous Topic Topic Next 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.02 seconds. Powered By: Snitz Forums 2000