-5

Let's say i have a string.

"Hello this is a "string need" to split"

I need to get he output as

Hello
this
is
a
string need
to 
split

Any help would be appreciated.

Aaron Bertrand
  • 272,866
  • 37
  • 466
  • 490
  • 1
    Are you using Oracle or sql server? You have tagged both and they are not the same thing. – Sean Lange Nov 20 '14 at 14:45
  • Lets close it. Seems people really hate he search function.... this is the 2nd time today I see someone just opening that same question again. – TomTom Nov 20 '14 at 14:58

1 Answers1

-1
CREATE FUNCTION [dbo].[fnSplitString] 
( 
    @string NVARCHAR(MAX), 
    @delimiter CHAR(1) 
) 
RETURNS @output TABLE(splitdata NVARCHAR(MAX) 
) 
BEGIN 
    DECLARE @start INT, @end INT 
    SELECT @start = 1, @end = CHARINDEX(@delimiter, @string) 
    WHILE @start < LEN(@string) + 1 BEGIN 
        IF @end = 0  
            SET @end = LEN(@string) + 1

        INSERT INTO @output (splitdata)  
        VALUES(SUBSTRING(@string, @start, @end - @start)) 
        SET @start = @end + 1 
        SET @end = CHARINDEX(@delimiter, @string, @start)

    END 
    RETURN 
END

This will Split the string into Words, from there you have to be more specific on what substring you want from the initial string.

This is for SQL Server.

CiucaS
  • 2,010
  • 5
  • 36
  • 63
  • 2
    Performance wise this is about the slowest way to split a string in sql server. Here are a few alternate options which perform far better. http://sqlperformance.com/2012/07/t-sql-queries/split-strings – Sean Lange Nov 20 '14 at 15:03
  • Still it works, I can't find a reson why someone downvoted my answer. – CiucaS Nov 20 '14 at 15:25
  • It does work, albeit quite slowly. Not sure it deserved a downvote though. That is pretty harsh. I will give you an upvote to offset somebody else downvoting with no explanation. – Sean Lange Nov 20 '14 at 15:37
  • 1
    @Sean The unmodified "really, really most recent, this time for sure" function from Moden still failed to compete with several of my approaches performance-wise. He's been claiming since I published that he's going to invalidate my tests and produce a counter, but still hasn't, reverting to ad hominem attacks instead. You'll also see that I strongly advocate TVPs instead of splitting at all. And "an upvote to offset" completely undermines the point of giving the community the ability to downvote at all. Would you have up-voted this answer if someone hadn't down-voted? If not, you've broken it. – Aaron Bertrand Nov 20 '14 at 16:42
  • (Links that didn't fit above - [testing Moden's unaltered function that only supports 8kb strings](http://sqlperformance.com/2012/08/t-sql-queries/splitting-strings-now-with-less-t-sql), [advocating for TVPs instead of splitting at all](http://sqlperformance.com/2012/08/t-sql-queries/splitting-strings-now-with-less-t-sql).) – Aaron Bertrand Nov 20 '14 at 16:43
  • 1
    @CiucaS probably for the same reason you wouldn't be congratulated for telling someone the fastest way to England is by rowboat. Just because something compiles and produces the right results does not automatically make it a good solution. – Aaron Bertrand Nov 20 '14 at 16:46
  • @AaronBertrand good point about the upvote, I guess I broke it and can't change it now. I agree that a TVP is a much better approach. I am not going to get involved in a disgreement between the two of you. In fact you will notice I posted a link to your article instead of his. :) – Sean Lange Nov 20 '14 at 17:16
  • @Sean sure, thanks, the other comment where you mentioned Moden explicitly has been removed, but that was the one I was replying to. – Aaron Bertrand Nov 20 '14 at 17:30
  • @AaronBertrand I didn't delete that one so not sure what happened to it. – Sean Lange Nov 20 '14 at 18:02
  • @AaronBertrand no one said it's was the optimal solution, still it works. I don't see how this solution can't be classified as good? I use it for quite some time and had no lag issues because of it. Let the user that asked the question to decide if it's a good solution for him or he/she needs a better one. – CiucaS Nov 20 '14 at 19:43