SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


String Split


String Split

Author
Message
falguni.thakor
falguni.thakor
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 13
Comments posted to this topic are about the item String Split
frederic.chapelon
frederic.chapelon
SSC Veteran
SSC Veteran (268 reputation)SSC Veteran (268 reputation)SSC Veteran (268 reputation)SSC Veteran (268 reputation)SSC Veteran (268 reputation)SSC Veteran (268 reputation)SSC Veteran (268 reputation)SSC Veteran (268 reputation)

Group: General Forum Members
Points: 268 Visits: 240
Yeah I used such function a lot in our applications although it is nice to have the ability to call with different delimiters (sometimes people don't like to use commas :-)...)
But recently I have been looking at it from a different point of view by using CLR functions and the speed of it is mind blowing compare to the T-SQL code.
Only limitation though is that the CLR function doesn't accept a string as long as the T-SQL one (or maybe I haven't found the correct configuration yet.
CLR might be very useful in this case so I wanted to give you the code if you want to try it.
Cheers

public partial class UserDefinedFunctions
{
[SqlFunction(Name = "ufn_SplitString", FillRowMethodName = "SplitString_FillRow", TableDefinition = "ID NVARCHAR(255)")]
public static IEnumerable SqlArray(SqlString str, SqlChars delimiter)
{
if (delimiter.Length == 0)
return new string[1] { str.Value };
return str.Value.Split(delimiter[0]);
}

public static void SplitString_FillRow(object row, out SqlString str)
{
str = new SqlString((string)row);
}
};
mstjean
mstjean
Mr or Mrs. 500
Mr or Mrs. 500 (597 reputation)Mr or Mrs. 500 (597 reputation)Mr or Mrs. 500 (597 reputation)Mr or Mrs. 500 (597 reputation)Mr or Mrs. 500 (597 reputation)Mr or Mrs. 500 (597 reputation)Mr or Mrs. 500 (597 reputation)Mr or Mrs. 500 (597 reputation)

Group: General Forum Members
Points: 597 Visits: 2550
Have a look at http://www.sqlservercentral.com/articles/Tally+Table/72993/
(Tally OH! An Improved SQL 8K “CSV Splitter” Function)

Especially, review the discussion thread. You can do this without committing that cardinal sin, looping (see references to the "Church of No RBAR" in the discussion). Performance is much improved when you avoid loops.


Cursors are useful if you don't know SQL
juned.katariya
juned.katariya
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 7
Nice PostSmooooth, worked like a charm for me :-D
Danny Ocean
Danny Ocean
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1844 Visits: 1549
You can also do this with help of below script:-

DECLARE @CommString Varchar(100) ='1,2,3,4,5,6,7,8,9,10'


SELECT CP_XML.Node.value('.','Varchar(max)') AS DATA FROM
(
SELECT DataIn_XML = Convert(XML,'<Node>'+REPLACE(@CommString,',','</Node><Node>')+'</Node>')
) AS NowDataIn_XmlFormat
CROSS APPLY DataIN_XML.nodes('Node') CP_XML(Node)

Thanks
Vinay Kumar
-----------------------------------------------------------------
Keep Learning - Keep Growing !!!
www.GrowWithSql.com
Iwas Bornready
Iwas Bornready
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16082 Visits: 885
Thanks for the script.
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