Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Function to Split a Delimited String into a Table


Function to Split a Delimited String into a Table

Author
Message
Andy B.
Andy B.
SSC-Enthusiastic
SSC-Enthusiastic (162 reputation)SSC-Enthusiastic (162 reputation)SSC-Enthusiastic (162 reputation)SSC-Enthusiastic (162 reputation)SSC-Enthusiastic (162 reputation)SSC-Enthusiastic (162 reputation)SSC-Enthusiastic (162 reputation)SSC-Enthusiastic (162 reputation)

Group: General Forum Members
Points: 162 Visits: 67
Comments posted to this topic are about the item Function to Split a Delimited String into a Table


Enjoy!

A Brown

Manage all of your backups in one simple job using this script.
philcart
philcart
SSCrazy
SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)

Group: General Forum Members
Points: 2752 Visits: 1434
SQL 2005 variant that doesn't use a loop.

http://philcart.blogspot.com/2007/06/split-function.html

Could be easily modified to work with nvarchar(4000)

Hope this helps
Phill Carter
--------------------
Colt 45 - the original point and click interface

Australian SQL Server User Groups - My profile
Phills Philosophies
Murrumbeena Cricket Club

Stylez
Stylez
SSC Journeyman
SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)

Group: General Forum Members
Points: 82 Visits: 579
Hi good Sir
thanks for doing the string split function. i would like to kindly ask if you have a sample code for my scenario below;

Column 1 = ID
Column 2 = Lastname
Column 3 = Job Description

and then i will declare a variable to be used in inserting the columns mentioned above

declare @Value varchar(4000)
set @value = 'IDValue,Lastnamevalue,JobDescriptionvalue'

i noticed that you are inserting these values in a single column. is it possible to save these values in a seperate column named ID,Lastname and Job Description? your assistance is very much appreciated.


Best Regards

N.O.E.L.
g.chakhidze
g.chakhidze
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
Points: 1 Visits: 14
Hi,
Thank you, I've found this function very useful.

There is a little typo at the line

SET @list = LTRIM(RTRIM(@list)) + ','

Instead of concatenating with default value (,), you must concatenate with passed in @delimiter argument.

SET @list = LTRIM(RTRIM(@list)) + @delimiter

Without this, if you use delimiter different than comma, it will not add last row to the resulting table.

For example:
select * from DStringToTable(N'one|two', N'|')
returns table

value
-----
one

which is missing value 'two'
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search