SQL Server split function

Credits due: look at the second post on http://searchvb.techtarget.com/tip/0,289483,sid8_gci932171,00.html
 
CREATE FUNCTION dbo.fnSplit(
@sInputList VARCHAR(8000) -- List of delimited items
, @sDelimiter VARCHAR(8000) = ',' -- delimiter that separates items
) RETURNS @List TABLE (item VARCHAR(8000))

BEGIN
DECLARE @sItem VARCHAR(8000)
WHILE CHARINDEX(@sDelimiter,@sInputList,0) <> 0
BEGIN
SELECT
@sItem=RTRIM(LTRIM(SUBSTRING(@sInputList,1,CHARINDEX(@sDelimiter,@sInputList,0)-1))),
@sInputList=RTRIM(LTRIM(SUBSTRING(@sInputList,CHARINDEX(@sDelimiter,@sInputList,0)+LEN(@sDelimiter),LEN(@sInputList))))

IF LEN(@sItem) > 0
INSERT INTO @List SELECT @sItem
END

IF LEN(@sInputList) > 0
INSERT INTO @List SELECT @sInputList -- Put the last item in
RETURN
END
GO
--Test
select * from fnSplit('1,22,333,444,,5555,666', ',')
select * from fnSplit('1##22#333##444','##') --note second item has embedded #
select * from fnSplit('1 22 333 444 5555 666', ' ')

posted @ Thursday, April 12, 2007 6:54 AM

Print

Comments on this entry:

# re: SQL Server split function

Left by Daniel at 1/16/2008 4:15 PM
Gravatar
Thanks, this is very useful.
/Daniel

# re: SQL Server split function

Left by Angel Eyes at 1/16/2008 4:32 PM
Gravatar
Thanks, Daniel.

# re: SQL Server split function

Left by bylove at 5/7/2008 10:48 PM
Gravatar
thanks

# re: SQL Server split function

Left by Rachana at 6/13/2008 9:14 AM
Gravatar
Thanks a lot

# re: SQL Server split function

Left by falguni at 7/15/2008 8:46 PM
Gravatar
Thanks a lot. Its very useful.

# to falguni

Left by angel eyes at 7/15/2008 8:59 PM
Gravatar
Glad to have helped :-)

# re: SQL Server split function

Left by angel eyes at 7/26/2008 10:04 AM
Gravatar
Sure thing.

# re: SQL Server split function

Left by Andrew McGimpsey at 7/29/2008 2:47 PM
Gravatar
This is great! :)

If I then want to get back only the nth row from the resulting table how would I get it? I would presume there is some sort of rownum function that we can use?

# re: get back only the nth row from the resulting table

Left by angel eyes at 7/29/2008 5:16 PM
Gravatar
Assuming you want to keep the resulting table, you can add RowNumber(), like this:
select ROW_NUMBER() over (order by item) as RowNum, * from fnSplit('1,22,333,444,,5555,666', ',')

And then get the 4th row like this:
select * from
(select ROW_NUMBER() over (order by item) as RowNum, * from fnSplit('1,22,333,444,,5555,666', ',')) tt
where RowNum = 4

# re: SQL Server split function

Left by gsd at 8/1/2008 6:51 AM
Gravatar
Good f(x)

# re: SQL Server split function

Left by Ali at 9/17/2008 7:37 AM
Gravatar
Hi
I was write this sp :

UPDATE TBLMessages
SET Location = @Location, FolderID = @FolderID
WHERE (dbo.TBLMessages.MsgID in (SELECT item FROM dbo.FnSplit(MsgID,',')))

But It Updates All Records !!!

# re: Ali

Left by angel eyes at 9/17/2008 9:03 AM
Gravatar
Ali, your query doesn't make much sense. what were you trying to do, and what does the MsgID field contain?

# re: SQL Server split function

Left by Matt at 9/26/2008 6:55 PM
Gravatar
If I were trying to use this to split a column that I select, how would I do that?

For example, say we have some customer numbers that are set up in a special fashion like 000-xxx-xxx, and others that are set up like normal numbers areacode-prefix-number, and I want to throw that into a case when and pull a list of how many users we have set up like that, kind of like this:

SELECT SUM(CASE WHEN (SELECT item FROM (SELECT ROW_NUMBER() OVER (ORDER BY item) AS RowNum, * FROM fnSplit(SELECT dbo.CUSTOMER.PHONE_NUM FROM dbo.CUSTOMER WHERE CUSTOMER.CUST_ID=1234, '-')) drvTbl WHERE RowNum = 1) THEN 1 ELSE 0 END) AS IRREGULAR_COUNT FROM dbo.CUSTOMER

The fnSplit function doesn't allow me to throw a select statement in it (or maybe I'm just missing something here).

Any ideas?

# re: SQL Server split function

Left by Matt at 9/30/2008 12:59 AM
Gravatar
Yea, I figured that out after banging my head against the wall for an hour or so. I have a tendancy to overthink things sometimes, haha.

Thanks for the reply, and the great blog.

# re: SQL Server split function

Left by Marcie at 10/15/2008 4:12 PM
Gravatar
Is there a way to use fnsplit on a column that is selected?

For example:
select * from dbo.fnsplit(select prov_title from provider where prov_title > ' ')

This doesn't work.

# Marcie

Left by angel eyes at 10/15/2008 8:24 PM
Gravatar
Hey Marcie.
To my knowledge, you cannot use the function like that, in a query.
You can use a cursor to do the same thing, but of course you'll have to watch the performance.

# re: SQL Server split function

Left by fatty0860 at 11/11/2008 9:33 AM
Gravatar
Thanks!
It's very useful to me.

# re: SQL Server split function

Left by Baljeet Singh at 12/29/2008 11:52 AM
Gravatar
Thanks a Lot.

# To: fatty0860 and Baljeet Singh

Left by Angel Eyes at 12/29/2008 8:17 PM
Gravatar
Sure, glad to be of help.

# re: SQL Server split function

Left by Devil at 3/5/2009 4:01 PM
Gravatar
Hello,

how can I use this function with a cursor?

I want to use this function like this example:
select * from dbo.fnsplit(select prov_title from provider where prov_title > ' ')

# re: SQL Server split function

Left by club penguin at 3/17/2009 2:44 PM
Gravatar
This tutorial should be put in a place of honor. The best!!!

# re: club penguin

Left by Angel Eyes at 3/26/2009 12:15 PM
Gravatar
Thanks, Club Penguin, it means a lot!

# re: SQL Server split function

Left by porno at 3/26/2009 1:39 PM
Gravatar
how can I use this function with a cursor?

# re: SQL Server split function

Left by porno izle at 3/26/2009 1:41 PM
Gravatar
Thanks!
It's very useful to me.

# Turkey Tourism

Left by tourist book at 3/26/2009 4:28 PM
Gravatar
Thanks for writing.but please letters be more big.:)

# re: SQL Server split function

Left by Diyarbakir at 3/26/2009 6:04 PM
Gravatar
Thanks you

# re: SQL Server split function

Left by pornoizle at 3/26/2009 6:20 PM
Gravatar
thanks you

# re: SQL Server split function

Left by oyun indir at 3/26/2009 7:23 PM
Gravatar
Thank you very useful information.

# re: SQL Server split function

Left by türk porno izle at 3/26/2009 7:58 PM
Gravatar
thanks a lot..

# re: SQL Server split function

Left by ali veli at 3/26/2009 9:07 PM
Gravatar
Very thanks.

# re: SQL Server split function

Left by Downloadic at 3/26/2009 9:29 PM
Gravatar
Very nice, thanks.....

# re: SQL Server split function

Left by SMF Destek at 3/26/2009 10:25 PM
Gravatar
Hı :) Thank you post...

SMF Destek

# re: SQL Server split function

Left by Porno Sikiş at 3/26/2009 10:31 PM
Gravatar
Thanks for articles.

# re: SQL Server split function

Left by free wordpress themes at 3/26/2009 10:56 PM
Gravatar
Sorry my english firstly, i read well but i cant write :), So its useful post for me and i bookmarked your blog.

wordpress themes

web tasarım

# re: tourist book

Left by Angel Eyes at 3/27/2009 12:16 PM
Gravatar
I'll change the fonts, or the whole theme :-)

# re: SQL Server split function

Left by worldmp3 at 3/27/2009 1:59 PM
Gravatar
Thank you for information.

# re: SQL Server split function

Left by Sezer DEGE at 3/28/2009 12:19 PM
Gravatar
useful article, many thanks.


Daily Linux News
web hosting

# re: SQL Server split function

Left by francisco at 4/10/2009 12:23 AM
Gravatar
i have modified to return item at specified index:

ALTER FUNCTION [dbo].[split](@sInputList VARCHAR(8000),@sDelimiter VARCHAR(8000) = ',',@Index INT)
RETURNS NVARCHAR(4000)
BEGIN
DECLARE @sItem VARCHAR(8000)
DECLARE @retval NVARCHAR(4000)
DECLARE @tempIndex INT
DECLARE @List TABLE(item VARCHAR(8000))
SET @tempIndex = 0
WHILE Charindex(@sDelimiter,@sInputList,0) <> 0
BEGIN
SELECT @sItem = Rtrim(Ltrim(Substring(@sInputList,1,Charindex(@sDelimiter,@sInputList,0) - 1))),
@sInputList = Rtrim(Ltrim(Substring(@sInputList,Charindex(@sDelimiter,@sInputList,0) + Len(@sDelimiter),
Len(@sInputList))))
IF Len(@sItem) > 0
INSERT INTO @List
SELECT @sItem
IF (@tempIndex = @Index)
SET @retval = @sItem
SET @tempIndex = @tempIndex + 1
END
IF Len(@sInputList) > 0
INSERT INTO @List
SELECT @sInputList -- Put the last item in
IF (@tempIndex = @Index)
SET @retval = @sItem
RETURN @retval
END
GO

# re: SQL Server split function

Left by re: francisco at 4/10/2009 11:09 AM
Gravatar
Way to go, Fransisco, Thanks for letting us know about it!

# re: SQL Server split function

Left by Tobar at 4/22/2009 6:46 PM
Gravatar
This doesn't work if space is your actual separator. ie there should be a NULL item, ala the first test, in the third test. As written extra spaces are discarded.

# re: SQL Server split function

Left by Tobar at 4/22/2009 8:55 PM
Gravatar
By "this" I was referring to the original post.

Here is a fix. Why in the world MS thought it smart to not count a space in the len() function is beyond me.

drop function dbo.fnSplit

go

CREATE FUNCTION dbo.fnSplit(
@sInputList VARCHAR(8000) -- List of delimited items
, @sDelimiter VARCHAR(8000) = ',' -- delimiter that separates items
) RETURNS @List TABLE (item VARCHAR(8000))

BEGIN
DECLARE @sItem VARCHAR(8000)
WHILE CHARINDEX(@sDelimiter,@sInputList,0) <> 0
BEGIN
SELECT
@sItem=SUBSTRING(@sInputList,1,CHARINDEX(@sDelimiter,@sInputList,0)-1),
@sInputList=SUBSTRING(@sInputList,CHARINDEX(@sDelimiter,@sInputList,0)+DATALENGTH(@sDelimiter),DATALENGTH(@sInputList))

IF LEN(@sItem) > 0
INSERT INTO @List SELECT @sItem
ELSE IF LEN(@sItem) = 0
INSERT INTO @List SELECT NULL
END

IF LEN(@sInputList) > 0
INSERT INTO @List SELECT @sInputList -- Put the last item in
RETURN
END
GO
--Test
select * from fnSplit('1,22,333,444,,5555,666', ',')
select * from fnSplit('1##22#333##444','##') --note second item has embedded #
select * from fnSplit('1 22 333 444 5555 666', ' ')

# re: SQL Server split function

Left by Ali at 5/8/2009 6:44 PM
Gravatar
Thanks ,for articles.

# re: SQL Server split function

Left by konteyner at 6/6/2009 7:31 AM
Gravatar
Thanks for...

# re: SQL Server split function

Left by su deposu at 6/6/2009 7:34 AM
Gravatar
Thanks a lot.. ty.

# EMİRCAN OTO KİRALAMA ANKARA

Left by Cihan at 6/22/2009 8:34 AM
Gravatar
Thank you

# LÜKS OTO KİRALAMA ANKARA EMİRCAN

Left by Ciko at 6/22/2009 8:39 AM
Gravatar
mersi şekerim

# re: SQL Server split function

Left by Sunshine Coast Web Designer at 7/8/2009 7:21 AM
Gravatar
Nice work. Was just about to write this myself then thought I should see if somebody else had already posted a solution before I reinvented the wheel. Now that's the proper meaning of resuable code.

# re: SQL Server split function

Left by güncel blog at 7/8/2009 2:25 PM
Gravatar
thanks admins very nicess

# re: SQL Server split function

Left by video seyret at 7/8/2009 2:27 PM
Gravatar
thanks you goods

# re: SQL Server split function

Left by Lida at 7/18/2009 9:52 AM
Gravatar
Good on your

# re: SQL Server split function

Left by Lida at 7/18/2009 9:54 AM
Gravatar
THanks a lot

# re: SQL Server split function

Left by Lida at 7/18/2009 9:54 AM
Gravatar
Lot name a

# prefabrik fiyatları

Left by prefabrik fiyatlari at 8/7/2009 12:35 PM
Gravatar
Thanks for the reply, and the great blog.

# re: SQL Server split function

Left by Diyarbakir at 8/12/2009 10:10 PM
Gravatar
Thanks for the reply, and the great blog.

# re: SQL Server split function

Left by Urls at 8/12/2009 10:10 PM
Gravatar
Good on your

# re: SQL Server split function

Left by evden eve nakliyat at 9/9/2009 12:17 PM
Gravatar
very useful command.thanks for that

# re: SQL Server split function

Left by invention of lying online at 10/5/2009 2:34 PM
Gravatar
THanks a lot

Your comment:



 (will not be displayed)


 
 
 
 

Live Comment Preview:

 
«November»
SunMonTueWedThuFriSat
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345