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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 split function
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

tech_1
Posting Yak Master

105 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?

quote:

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

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

END

James K
Flowing Fount of Yak Knowledge

3562 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: http://www.sqlservercentral.com/articles/Tally+Table/72993/ The function is in Figure 21 of that article.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

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

http://visakhm.blogspot.in/2013/01/delimited-string-split-xml-parsing.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3562 Posts

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

also

http://visakhm.blogspot.in/2013/01/delimited-string-split-xml-parsing.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs


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  
 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.09 seconds. Powered By: Snitz Forums 2000