506

Using SQL Server, how do I split a string so I can access item x?

Take a string "Hello John Smith". How can I split the string by space and access the item at index 1 which should return "John"?

Shnugo
  • 62,351
  • 7
  • 42
  • 92
GateKiller
  • 68,419
  • 71
  • 167
  • 203
  • 3
    See http://stackoverflow.com/questions/314824/t-sql-opposite-to-string-concatenation-how-to-split-string-into-multiple-recor as well – Jarrod Dixon Mar 08 '10 at 19:44
  • 5
    built-in as of sql server 2016 https://msdn.microsoft.com/en-us/library/mt684588.aspx – Tim Abell Jun 24 '16 at 10:02
  • 4
    The *highest* answers here are - at least for me - quite old fashioned and rather out-dated. Procedural locig, loops, recursions, CLR, functions, many lines of code... It might be interesting to read the "active" answers to find more *up-to-date* approaches. – Shnugo Jul 12 '16 at 10:18
  • I have added a new answer with more up-to-date approach: https://stackoverflow.com/a/49669994/632604 – Gorgi Rankovski Apr 05 '18 at 10:25
  • Try **Get the nth element of a list** -> https://portosql.wordpress.com/2019/05/27/enesimo-elemento-lista/ – José Diz Sep 03 '19 at 20:45
  • Some comments (including one of mine) have been deleted. Any of the moderators care to comment? – Dave Mason Apr 18 '20 at 19:40
  • 1
    @TimAbell, the documentation states that "The order is not guaranteed to match the order of the substrings in the input string". – Greg Jan 06 '21 at 16:33

44 Answers44

360

I don't believe SQL Server has a built-in split function, so other than a UDF, the only other answer I know is to hijack the PARSENAME function:

SELECT PARSENAME(REPLACE('Hello John Smith', ' ', '.'), 2) 

PARSENAME takes a string and splits it on the period character. It takes a number as its second argument, and that number specifies which segment of the string to return (working from back to front).

SELECT PARSENAME(REPLACE('Hello John Smith', ' ', '.'), 3)  --return Hello

Obvious problem is when the string already contains a period. I still think using a UDF is the best way...any other suggestions?

Luke Girvin
  • 12,672
  • 8
  • 57
  • 79
Nathan Bedford
  • 8,394
  • 5
  • 32
  • 29
  • 103
    Thanks Saul...I should point out that this solution is really a bad solution for real development. PARSENAME only expects four parts, so using a string with more than four parts causes it to return NULL. The UDF solutions are obviously better. – Nathan Bedford Jul 01 '09 at 15:54
  • 33
    This is a great hack, and also makes me weep that something like this is necessary for something so friggin simple in real languages. – Factor Mystic Jul 12 '10 at 14:09
  • 36
    To make the indexes work in the "right" way, that is, starting at 1, i've hijacked your hijack with REVERSE: REVERSE(PARSENAME(REPLACE(REVERSE('Hello John Smith'), ' ', '.'), 1)) -- Returns Hello – NothingsImpossible May 14 '12 at 13:57
  • 3
    @FactorMystic [First Normal Form](http://en.wikipedia.org/wiki/First_normal_form) requires that you not put multiple values in a single field. It's literally the first rule of an RDBMS. A `SPLIT()` function is not supplied because it encourages poor database design, and the database will never be optimized to use data stored in this format. The RDBMS is not obligated to help developers do stupid things that it has been designed *not* to handle. The correct answer will *always* be "Normalize your database like we told you 40 years ago." Neither SQL nor the RDBMS are to blame for poor design. – Bacon Bits Dec 08 '14 at 04:11
  • This is uber cool. It just so happens that the data i'm working with has four elements – MrBliz Jun 25 '15 at 11:57
  • +1 can always concoct a quick and dirty working solution for >4 tokens with a combination of reverse(), charindex(), substring() – hello_earth Feb 08 '16 at 09:09
  • 9
    @BaconBits while I agree in theory, in practice tools like this are useful when normalizing a poor design produced by someone who came before you. – Tim Abell Jun 24 '16 at 09:34
  • When the number of splits in the string varies from left to right, then the @NothingsImpossible route is the way to go . – wwmbes Oct 27 '16 at 17:14
  • @NothingsImpossible The performance of the REVERSE(PARSENAME(REPLACE(REVERSE('Hello John Smith'), ' ', '.'), 1)) solution is excellent and the 4 field limitation fits my problem. Thanks!!! – wwmbes Oct 28 '16 at 07:23
  • @hello_earth I'd love to see what you come up with. Please show us. – wwmbes Oct 28 '16 at 07:29
  • @wwmbes i posted a separate answer - it's very quick and dirty and ugly, and stems from the same principles but anyway, since you asked. cheers – hello_earth Oct 31 '16 at 14:20
  • 1
    For awareness: Since parsename function is designed to return database identifiers, the value returned is limited to 128 characters (a sysname data type, which corresponds to nvarchar(128) ). If it goes beyond this, a NULL will be returned instead. – Kyle Weller May 30 '17 at 20:31
  • For awareness: it works only with four elements or less. If you have more than four elements the PARSENAME always return NULL, even for index lower than 5. https://docs.microsoft.com/it-it/sql/t-sql/functions/parsename-transact-sql?view=sql-server-2017 – aKiRa May 24 '18 at 13:22
192

You may find the solution in SQL User Defined Function to Parse a Delimited String helpful (from The Code Project).

You can use this simple logic:

Declare @products varchar(200) = '1|20|3|343|44|6|8765'
Declare @individual varchar(20) = null

WHILE LEN(@products) > 0
BEGIN
    IF PATINDEX('%|%', @products) > 0
    BEGIN
        SET @individual = SUBSTRING(@products,
                                    0,
                                    PATINDEX('%|%', @products))
        SELECT @individual

        SET @products = SUBSTRING(@products,
                                  LEN(@individual + '|') + 1,
                                  LEN(@products))
    END
    ELSE
    BEGIN
        SET @individual = @products
        SET @products = NULL
        SELECT @individual
    END
END
Nhan
  • 3,422
  • 6
  • 28
  • 35
Jonesinator
  • 4,083
  • 2
  • 21
  • 18
  • 1
    why `SET @p_SourceText = RTRIM( LTRIM( @p_SourceText)) SET @w_Length = DATALENGTH( RTRIM( LTRIM( @p_SourceText)))` and not `SET @p_SourceText = RTRIM( LTRIM( @p_SourceText)) SET @w_Length = DATALENGTH( @p_SourceText)`? – Beth Sep 29 '10 at 15:13
  • 12
    @GateKiller This solution does not support Unicode & it uses hard coded numeric(18,3) which doesn't make it a viable "reusable" function. – Filip De Vos Mar 18 '11 at 13:55
  • 4
    This works but allocates a lot of memory and wastes CPU. – jjxtra May 26 '15 at 16:56
  • 2
    As of SQL Server 2016, there is now a built-in function [`STRING_SPLIT`](https://docs.microsoft.com/en-us/sql/t-sql/functions/string-split-transact-sql) that will split a string and return a one-column table result which you can use in a `SELECT` statement or elsewhere. – qJake Apr 03 '17 at 20:24
  • Too bad the guys I work for aren't on 2016. But, I'll keep it in mind in case they ever get the lead out of their shoes. Great solution in the interim. I implemented it as a function and and added delimiter as an argument. – Brandon Griffin May 02 '17 at 19:38
  • @Jonesinator Thanks. It is the second time I need this and come here. And at last have make a SplitString Table Valued Function with this for Sql server 2008, 2010, 2014 [Here The SplitString Function And it's calling mechanism](https://submitmysites.blogspot.com/2016/09/details-of-split-string-in-sql-sql.html) – Muhammad Ashikuzzaman Nov 19 '18 at 16:06
  • @qJake How would you use `STRING_SPLIT` with an index? The question asks how to retrieve a specific value, not a full list. – Stevoisiak Aug 05 '20 at 16:25
  • can you help me with how this logic can be changed if the input = |1|20|3|343|44|6|8765| – aditya Oct 06 '20 at 05:42
110

First, create a function (using CTE, common table expression does away with the need for a temp table)

 create function dbo.SplitString 
    (
        @str nvarchar(4000), 
        @separator char(1)
    )
    returns table
    AS
    return (
        with tokens(p, a, b) AS (
            select 
                1, 
                1, 
                charindex(@separator, @str)
            union all
            select
                p + 1, 
                b + 1, 
                charindex(@separator, @str, b + 1)
            from tokens
            where b > 0
        )
        select
            p-1 zeroBasedOccurance,
            substring(
                @str, 
                a, 
                case when b > 0 then b-a ELSE 4000 end) 
            AS s
        from tokens
      )
    GO

Then, use it as any table (or modify it to fit within your existing stored proc) like this.

select s 
from dbo.SplitString('Hello John Smith', ' ')
where zeroBasedOccurance=1

Update

Previous version would fail for input string longer than 4000 chars. This version takes care of the limitation:

create function dbo.SplitString 
(
    @str nvarchar(max), 
    @separator char(1)
)
returns table
AS
return (
with tokens(p, a, b) AS (
    select 
        cast(1 as bigint), 
        cast(1 as bigint), 
        charindex(@separator, @str)
    union all
    select
        p + 1, 
        b + 1, 
        charindex(@separator, @str, b + 1)
    from tokens
    where b > 0
)
select
    p-1 ItemIndex,
    substring(
        @str, 
        a, 
        case when b > 0 then b-a ELSE LEN(@str) end) 
    AS s
from tokens
);

GO

Usage remains the same.

vzczc
  • 8,522
  • 5
  • 49
  • 58
  • 14
    It's elegant but only works for 100 elements because of the limit of recursion depth. – Pking Nov 07 '12 at 15:31
  • 4
    @Pking, no, the default is `100` (to prevent infinite loop). Use [MAXRECURSION hint](http://msdn.microsoft.com/en-us/library/ms175972%28v=sql.90%29.aspx) to define number of recursion levels (`0` to `32767`, `0` is "no limit" - may crush server). BTW, much better answer than `PARSENAME`, because it's universal :-). +1 – Michał Powaga Mar 14 '13 at 14:45
  • Adding `maxrecursion` to this solution keep in mind this question and its answers [How to setup the `maxrecursion` option for a CTE inside a Table-Valued-Function](http://stackoverflow.com/questions/7428669/how-to-setup-the-maxrecursion-option-for-a-cte-inside-a-table-valued-function). – Michał Powaga Mar 15 '13 at 09:03
  • Specifically, reference [the answer by Crisfole](http://stackoverflow.com/a/10938339/1225845) - his method slows it somewhat, but is simpler than most other options. – AHiggins Jul 30 '15 at 18:05
  • minor point but the usage doesn't remain the same because you changed the column name, so `s` is no longer defined – Tim Abell Jun 24 '16 at 09:45
  • I avoid the recursion limitation by using this code, which if I were not happily moving to SQL Server 2016 I would make a table-valued function or a scalar function, depending on my actual needs. By looping instead of self-joining you could theoretically use any size string. – Joey Morgan Feb 11 '20 at 21:15
67

Most of the solutions here use while loops or recursive CTEs. A set-based approach will be superior, I promise, if you can use a delimiter other than a space:

CREATE FUNCTION [dbo].[SplitString]
    (
        @List NVARCHAR(MAX),
        @Delim VARCHAR(255)
    )
    RETURNS TABLE
    AS
        RETURN ( SELECT [Value], idx = RANK() OVER (ORDER BY n) FROM 
          ( 
            SELECT n = Number, 
              [Value] = LTRIM(RTRIM(SUBSTRING(@List, [Number],
              CHARINDEX(@Delim, @List + @Delim, [Number]) - [Number])))
            FROM (SELECT Number = ROW_NUMBER() OVER (ORDER BY name)
              FROM sys.all_objects) AS x
              WHERE Number <= LEN(@List)
              AND SUBSTRING(@Delim + @List, [Number], LEN(@Delim)) = @Delim
          ) AS y
        );

Sample usage:

SELECT Value FROM dbo.SplitString('foo,bar,blat,foo,splunge',',')
  WHERE idx = 3;

Results:

----
blat

You could also add the idx you want as an argument to the function, but I'll leave that as an exercise to the reader.

You can't do this with just the native STRING_SPLIT function added in SQL Server 2016, because there is no guarantee that the output will be rendered in the order of the original list. In other words, if you pass in 3,6,1 the result will likely be in that order, but it could be 1,3,6. I have asked for the community's help in improving the built-in function here:

With enough qualitative feedback, they may actually consider making some of these enhancements:

More on split functions, why (and proof that) while loops and recursive CTEs don't scale, and better alternatives, if splitting strings coming from the application layer:

On SQL Server 2016 or above, though, you should look at STRING_SPLIT() and STRING_AGG():

Aaron Bertrand
  • 242,666
  • 35
  • 420
  • 451
  • 1
    Best answer, IMHO. In some of other answers there is the issue of SQL recursion limit of 100, but not in this case. Very fast and very simple implementation. Where is the +2 button? – T-moty Oct 21 '15 at 15:01
  • Thanks to Aaron; But can someone explain why if I pass `varchar` (not `varchar(max)`) as the argument this function returns empty list? like `declare @list varchar = 'something'; select from dbo.SplitString(@list, ';');` – Mikhail Boyarsky Sep 23 '16 at 13:20
  • @Mikhail Because `varchar` without length can either be `varchar(30)` or `varchar(1)` depending on context. Don't try to understand that problem - [just don't use that syntax. Ever](http://sqlblog.com/blogs/aaron_bertrand/archive/2009/10/09/bad-habits-to-kick-declaring-varchar-without-length.aspx). – Aaron Bertrand Sep 23 '16 at 20:08
  • 5
    I tried this function verbatim with the usage: `select * from DBO.SplitString('Hello John smith', ' ');` and the output produced was: **Value** Hello ello llo lo o John ohn hn n smith mith ith th h – wwmbes Oct 11 '16 at 10:27
  • @wwmbes Try using a delimiter other than space. Many functions are going to have the issue that trailing spaces are dropped. – Aaron Bertrand Oct 11 '16 at 15:07
  • 2
    @AaronBertrand The original problem posted by GateKiller involves a space delimiter. – wwmbes Oct 26 '16 at 12:00
  • The "len" function has an implict built-in "rtrim" which comes from the 80's when SQL only had char (not varchar) so try this: `select len( ' ' ) as len_space, len( '* ' ) as len_star_space, len( ' *' ) as len_space_star, datalength( ' ' ) as datalength_space, datalength( '* ' ) as datalength_star_space, datalength( ' *' ) as datalength_space_star, case when '' = ' ' then 'yes' else 'no' end as nospace_equals_space` Fix: replace "len" with "datalength" (but beware n(var)char) – Alasdair C-S Oct 22 '19 at 13:20
  • This splits the string. how do you retrieve a specific n-th element? – user1255933 Dec 13 '19 at 23:48
  • 1
    @user1255933 Addressed. – Aaron Bertrand Dec 17 '19 at 21:01
  • @AaronBertrand Thanks very much, I can reproduce this. – user1255933 Dec 19 '19 at 19:43
  • @AaronBertrand adding example for applying to a whole table: SELECT o.myfield, u.value from mytable o cross apply dbo.SplitString(o.myfield,';') u where u.idx = 4 – user1255933 Dec 19 '19 at 20:17
  • This won't work if you don't have "CREATE FUNCTION" permission... – Michael Feb 26 '20 at 23:58
  • 1
    @Michael Yes, that’s true. You also wouldn’t have a table to select from if you didn’t have ALTER SCHEMA permission, and wouldn’t be able to select from it if you don’t have SELECT permission You could always _ask someone_ to create the function for you. Or create it somewhere you can create it (even temporarily, say in tempdb). And on 2016+ you should be using STRING_SPLIT() and not a function you have to create yourself anyway. – Aaron Bertrand Feb 27 '20 at 00:19
  • @AaronBertrand I'm on 2016, but apparently not compatibility level >= 130 (the db admin wasn't sure and won't be able to look into it until later) – Michael Feb 27 '20 at 03:41
  • @AaronBertrand "And on 2016+ you should be using STRING_SPLIT()" : I can't imagine any real-world use for STRING_SPLIT, since it does not return an index for the parts of the string that was split, and according to [it's docs](https://docs.microsoft.com/en-us/sql/t-sql/functions/string-split-transact-sql), "the order of the output may vary as the order is not guaranteed to match the order of the substrings in the input string". – Reversed Engineer Apr 08 '20 at 14:30
  • I like the way [this Azure Feedback item](https://feedback.azure.com/forums/908035-sql-server/suggestions/32902852-string-split-is-not-feature-complete) puts it: 'STRING_SPLIT is not feature complete', and 'It’s a shame that this was submitted as just a “suggestion”. It should actually be listed as a “bug” because there’s only a comparatively small set of use cases where enumeration of the result set of elements is not important.' – Reversed Engineer Apr 08 '20 at 14:35
  • 1
    @ReversedEngineer There are plenty of uses that don't (and shouldn't) care about the original order of the list. "Find all the customers in this list." Why does it matter if the list is `3,5,2,8` or `8,3,5,2`? I think it's a rather obscure edge case that the requirement would be "Find all the customers in this list _and_ render them in exactly this order." (I'm not arguing against fixing that, I'm just suggesting that my impression of the majority of uses for the function differs from yours. That is anecdotal evidence from the number of accepted answers showing functions without ordinal out.) – Aaron Bertrand Apr 08 '20 at 16:44
  • 1
    @ReversedEngineer Anyway, updated my answer with [some of my thoughts around this](https://sqlperformance.com/2020/03/t-sql-queries/please-help-string-split-improvements). Also, as an aside, I was one of the ones who pushed for clarity in the docs, that output order may not match input order. – Aaron Bertrand Apr 08 '20 at 16:54
  • @AaronBertrand "Also, as an aside, I was one of the ones who pushed for clarity in the docs, that output order may not match input order." - **Thank you!** I appreciate what you do to "press Microsoft for improvements", as well as help clear up this kind of vague "Microsoft documentation for Dummies". Also I see your point about there being cases when the order doesn't matter, in which case STRING_SPLIT is indeed a great solution. – Reversed Engineer Apr 09 '20 at 09:32
38

You can leverage a Number table to do the string parsing.

Create a physical numbers table:

    create table dbo.Numbers (N int primary key);
    insert into dbo.Numbers
        select top 1000 row_number() over(order by number) from master..spt_values
    go

Create test table with 1000000 rows

    create table #yak (i int identity(1,1) primary key, array varchar(50))

    insert into #yak(array)
        select 'a,b,c' from dbo.Numbers n cross join dbo.Numbers nn
    go

Create the function

    create function [dbo].[ufn_ParseArray]
        (   @Input      nvarchar(4000), 
            @Delimiter  char(1) = ',',
            @BaseIdent  int
        )
    returns table as
    return  
        (   select  row_number() over (order by n asc) + (@BaseIdent - 1) [i],
                    substring(@Input, n, charindex(@Delimiter, @Input + @Delimiter, n) - n) s
            from    dbo.Numbers
            where   n <= convert(int, len(@Input)) and
                    substring(@Delimiter + @Input, n, 1) = @Delimiter
        )
    go

Usage (outputs 3mil rows in 40s on my laptop)

    select * 
    from #yak 
    cross apply dbo.ufn_ParseArray(array, ',', 1)

cleanup

    drop table dbo.Numbers;
    drop function  [dbo].[ufn_ParseArray]

Performance here is not amazing, but calling a function over a million row table is not the best idea. If performing a string split over many rows I would avoid the function.

Nathan Skerl
  • 8,382
  • 3
  • 35
  • 52
  • 2
    The best solution IMO, the others have some kind of limitation.. this is fast and can parse long strings with many elements. – Pking Dec 06 '12 at 13:01
  • Why do you order n descending? If there where three items, and we started numbering at 1, then the first item will be number 3, and the last will be number 1. Wouldn't it give more intuitive results if the `desc` were removed? – hatchet - done with SOverflow Oct 28 '14 at 16:13
  • 1
    Agreed, would be more intuitive in the asc direction. I was following parsename() convention which uses desc – Nathan Skerl Oct 28 '14 at 17:43
  • 3
    some explanation as to how this works would be great – Tim Abell Jun 24 '16 at 09:46
  • In a test on 100 million rows of up to 3 fields to parse, ufn_ParseArray did not finish after 25 minutes, while `REVERSE(PARSENAME(REPLACE(REVERSE('Hello John Smith'), ' ', '.'), 1))` from @NothingsImpossible completed in 1.5min. @hello_earth How would your solution compare on longer strings with more than 4 fields? – wwmbes Oct 28 '16 at 07:41
  • On further investigation, when the @NothingsImpossible version is embedded in a function and used from there, it performs 25 times worse than when used directly in a query. Can anyone comment on why? – wwmbes Oct 28 '16 at 16:23
  • @wwmbes Try using a physical number table with a clustered index. The usage of master..spt_values is just for illustration – Nathan Skerl Oct 28 '16 at 17:49
  • @wwmbes added a physical number table example – Nathan Skerl Oct 28 '16 at 18:06
35

This question is not about a string split approach, but about how to get the nth element.

All answers here are doing some kind of string splitting using recursion, CTEs, multiple CHARINDEX, REVERSE and PATINDEX, inventing functions, call for CLR methods, number tables, CROSS APPLYs ... Most answers cover many lines of code.

But - if you really want nothing more than an approach to get the nth element - this can be done as real one-liner, no UDF, not even a sub-select... And as an extra benefit: type safe

Get part 2 delimited by a space:

DECLARE @input NVARCHAR(100)=N'part1 part2 part3';
SELECT CAST(N'<x>' + REPLACE(@input,N' ',N'</x><x>') + N'</x>' AS XML).value('/x[2]','nvarchar(max)')

Of course you can use variables for delimiter and position (use sql:column to retrieve the position directly from a query's value):

DECLARE @dlmt NVARCHAR(10)=N' ';
DECLARE @pos INT = 2;
SELECT CAST(N'<x>' + REPLACE(@input,@dlmt,N'</x><x>') + N'</x>' AS XML).value('/x[sql:variable("@pos")][1]','nvarchar(max)')

If your string might include forbidden characters (especially one among &><), you still can do it this way. Just use FOR XML PATH on your string first to replace all forbidden characters with the fitting escape sequence implicitly.

It's a very special case if - additionally - your delimiter is the semicolon. In this case I replace the delimiter first to '#DLMT#', and replace this to the XML tags finally:

SET @input=N'Some <, > and &;Other äöü@€;One more';
SET @dlmt=N';';
SELECT CAST(N'<x>' + REPLACE((SELECT REPLACE(@input,@dlmt,'#DLMT#') AS [*] FOR XML PATH('')),N'#DLMT#',N'</x><x>') + N'</x>' AS XML).value('/x[sql:variable("@pos")][1]','nvarchar(max)');

UPDATE for SQL-Server 2016+

Regretfully the developers forgot to return the part's index with STRING_SPLIT. But, using SQL-Server 2016+, there is JSON_VALUE and OPENJSON.

With JSON_VALUE we can pass in the position as the index' array.

For OPENJSON the documentation states clearly:

When OPENJSON parses a JSON array, the function returns the indexes of the elements in the JSON text as keys.

A string like 1,2,3 needs nothing more than brackets: [1,2,3].
A string of words like this is an example needs to be ["this","is","an","example"].
These are very easy string operations. Just try it out:

DECLARE @str VARCHAR(100)='Hello John Smith';
DECLARE @position INT = 2;

--We can build the json-path '$[1]' using CONCAT
SELECT JSON_VALUE('["' + REPLACE(@str,' ','","') + '"]',CONCAT('$[',@position-1,']'));

--See this for a position safe string-splitter (zero-based):

SELECT  JsonArray.[key] AS [Position]
       ,JsonArray.[value] AS [Part]
FROM OPENJSON('["' + REPLACE(@str,' ','","') + '"]') JsonArray

In this post I tested various approaches and found, that OPENJSON is really fast. Even much faster than the famous "delimitedSplit8k()" method...

UPDATE 2 - Get the values type-safe

We can use an array within an array simply by using doubled [[]]. This allows for a typed WITH-clause:

DECLARE  @SomeDelimitedString VARCHAR(100)='part1|1|20190920';

DECLARE @JsonArray NVARCHAR(MAX)=CONCAT('[["',REPLACE(@SomeDelimitedString,'|','","'),'"]]');

SELECT @SomeDelimitedString          AS TheOriginal
      ,@JsonArray                    AS TransformedToJSON
      ,ValuesFromTheArray.*
FROM OPENJSON(@JsonArray)
WITH(TheFirstFragment  VARCHAR(100) '$[0]'
    ,TheSecondFragment INT          '$[1]'
    ,TheThirdFragment  DATE         '$[2]') ValuesFromTheArray
Community
  • 1
  • 1
Shnugo
  • 62,351
  • 7
  • 42
  • 92
  • Re: if your string might include forbidden characters... you could simply wrap the substrings like so `x]]>`. – Salman A Jan 28 '19 at 07:26
  • @SalmanA, yeah ,`CDATA`-sections can deal with this too... But after the cast they are gone (changed to escaped `text()` implicitly). I do not like *magic under the hood*, so I'd prefer the `(SELECT 'Text with ' AS [*] FOR XML PATH(''))` - approach. This looks cleaner to me and happens anyway... (Some more [about CDATA and XML](https://stackoverflow.com/a/39034049/5089204)). – Shnugo Jan 28 '19 at 07:32
22

Here is a UDF which will do it. It will return a table of the delimited values, haven't tried all scenarios on it but your example works fine.


CREATE FUNCTION SplitString 
(
    -- Add the parameters for the function here
    @myString varchar(500),
    @deliminator varchar(10)
)
RETURNS 
@ReturnTable TABLE 
(
    -- Add the column definitions for the TABLE variable here
    [id] [int] IDENTITY(1,1) NOT NULL,
    [part] [varchar](50) NULL
)
AS
BEGIN
        Declare @iSpaces int
        Declare @part varchar(50)

        --initialize spaces
        Select @iSpaces = charindex(@deliminator,@myString,0)
        While @iSpaces > 0

        Begin
            Select @part = substring(@myString,0,charindex(@deliminator,@myString,0))

            Insert Into @ReturnTable(part)
            Select @part

    Select @myString = substring(@mystring,charindex(@deliminator,@myString,0)+ len(@deliminator),len(@myString) - charindex(' ',@myString,0))


            Select @iSpaces = charindex(@deliminator,@myString,0)
        end

        If len(@myString) > 0
            Insert Into @ReturnTable
            Select @myString

    RETURN 
END
GO

You would call it like this:


Select * From SplitString('Hello John Smith',' ')

Edit: Updated solution to handle delimters with a len>1 as in :


select * From SplitString('Hello**John**Smith','**')
brendan
  • 27,495
  • 18
  • 64
  • 106
  • Didn't work for select * from dbo.ethos_SplitString_fn('guy,wicks,was here',',') id part ----------- -------------------------------------------------- 1 guy 2 wick – Guy Oct 20 '08 at 15:25
  • 2
    watch out with len() as it'll not return correct number if its argument has trailing spaces., e.g. len(' - ') = 2. – Rory Oct 17 '09 at 16:30
  • Doesn't work on: select * from dbo.SplitString('foo,foo test,,,,foo',',') – cbp Apr 14 '10 at 05:14
  • 1
    Fix for cbp.. Select @myString = substring(@mystring,@iSpaces + len(@deliminator),len(@myString) - charindex(@deliminator,@myString,0)) – Alxwest May 21 '12 at 10:12
16

Here I post a simple way of solution

CREATE FUNCTION [dbo].[split](
          @delimited NVARCHAR(MAX),
          @delimiter NVARCHAR(100)
        ) RETURNS @t TABLE (id INT IDENTITY(1,1), val NVARCHAR(MAX))
        AS
        BEGIN
          DECLARE @xml XML
          SET @xml = N'<t>' + REPLACE(@delimited,@delimiter,'</t><t>') + '</t>'

          INSERT INTO @t(val)
          SELECT  r.value('.','varchar(MAX)') as item
          FROM  @xml.nodes('/t') as records(r)
          RETURN
        END


Execute the function like this

  select * from dbo.split('Hello John Smith',' ')
Mudassir Hasan
  • 26,105
  • 18
  • 90
  • 124
10

In my opinion you guys are making it way too complicated. Just create a CLR UDF and be done with it.

using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.Collections.Generic;

public partial class UserDefinedFunctions {
  [SqlFunction]
  public static SqlString SearchString(string Search) {
    List<string> SearchWords = new List<string>();
    foreach (string s in Search.Split(new char[] { ' ' })) {
      if (!s.ToLower().Equals("or") && !s.ToLower().Equals("and")) {
        SearchWords.Add(s);
      }
    }

    return new SqlString(string.Join(" OR ", SearchWords.ToArray()));
  }
};
Damon Drake
  • 809
  • 1
  • 10
  • 23
  • 20
    I guess this is too much complicated, because I need to have Visual Studio, then enable CLR on the server, then create and compile the project, and finally add the assemblies to the database, in order to use it. But still is an interesting answer. – Guillermo Gutiérrez Sep 27 '12 at 13:55
  • 3
    @guillegr123, it doesn't have to be complicated. You can just download and install (for free!), SQL#, which is a library of SQLCLR functions and procs. You can get it from http://www.SQLsharp.com . Yes, I am the author but String_Split is included in the Free version. – Solomon Rutzky Aug 18 '13 at 16:18
10

What about using string and values() statement?

DECLARE @str varchar(max)
SET @str = 'Hello John Smith'

DECLARE @separator varchar(max)
SET @separator = ' '

DECLARE @Splited TABLE(id int IDENTITY(1,1), item varchar(max))

SET @str = REPLACE(@str, @separator, '''),(''')
SET @str = 'SELECT * FROM (VALUES(''' + @str + ''')) AS V(A)' 

INSERT INTO @Splited
EXEC(@str)

SELECT * FROM @Splited

Result-set achieved.

id  item
1   Hello
2   John
3   Smith
shA.t
  • 15,232
  • 5
  • 47
  • 95
Frederic
  • 998
  • 6
  • 11
  • 1
    i used your answer but did not work, but i modified and this worked with union all, i am using sql 2005 – angel Aug 13 '13 at 15:06
9

I use the answer of frederic but this did not work in SQL Server 2005

I modified it and I'm using select with union all and it works

DECLARE @str varchar(max)
SET @str = 'Hello John Smith how are you'

DECLARE @separator varchar(max)
SET @separator = ' '

DECLARE @Splited table(id int IDENTITY(1,1), item varchar(max))

SET @str = REPLACE(@str, @separator, ''' UNION ALL SELECT ''')
SET @str = ' SELECT  ''' + @str + '''  ' 

INSERT INTO @Splited
EXEC(@str)

SELECT * FROM @Splited

And the result-set is:

id  item
1   Hello
2   John
3   Smith
4   how
5   are
6   you
shA.t
  • 15,232
  • 5
  • 47
  • 95
angel
  • 3,986
  • 10
  • 52
  • 82
  • This is really great i've ever seen in sql stuff, it worked for my job and i appreciate that, thanks ! – Abdurrahman I. Mar 24 '16 at 08:36
  • I got really excited when I saw this because it looked so clean and easy to understand, but unfortunately you can't put this inside a UDF because of the `EXEC`. `EXEC` implicitly calls a stored procedure, and you can't use stored procedures in UDFs. – Kristen Hammack Aug 10 '16 at 13:20
  • This Works perfectly!! i was looking into using a function(SplitStrings_Moden) from here: https://sqlperformance.com/2012/07/t-sql-queries/split-strings#comments that does this and it was taking a minute and a half to split the data and return the rows when only using 4 account numbers. I tested your version with a left join on the table with the data on account numbers and it took like 2 or 3 seconds! Huge difference and works flawlessly! I'd give this 20 votes if possible! – MattE Mar 08 '19 at 18:57
8

This pattern works fine and you can generalize

Convert(xml,'<n>'+Replace(FIELD,'.','</n><n>')+'</n>').value('(/n[INDEX])','TYPE')
                          ^^^^^                                   ^^^^^     ^^^^

note FIELD, INDEX and TYPE.

Let some table with identifiers like

sys.message.1234.warning.A45
sys.message.1235.error.O98
....

Then, you can write

SELECT Source         = q.value('(/n[1])', 'varchar(10)'),
       RecordType     = q.value('(/n[2])', 'varchar(20)'),
       RecordNumber   = q.value('(/n[3])', 'int'),
       Status         = q.value('(/n[4])', 'varchar(5)')
FROM   (
         SELECT   q = Convert(xml,'<n>'+Replace(fieldName,'.','</n><n>')+'</n>')
         FROM     some_TABLE
       ) Q

splitting and casting all parts.

josejuan
  • 7,057
  • 19
  • 23
  • This is the only solution here which allows you to cast to specific types, and is moderately efficient (CLR still is most efficient, but this approach handles an 8gb, 10 token, 10M row table in about 9 mins (aws m3 server, 4k iops provisioned drive) – Andrew Hill Dec 08 '14 at 03:53
7

Yet another get n'th part of string by delimeter function:

create function GetStringPartByDelimeter (
    @value as nvarchar(max),
    @delimeter as nvarchar(max),
    @position as int
) returns NVARCHAR(MAX) 
AS BEGIN
    declare @startPos as int
    declare @endPos as int
    set @endPos = -1
    while (@position > 0 and @endPos != 0) begin
        set @startPos = @endPos + 1
        set @endPos = charindex(@delimeter, @value, @startPos)

        if(@position = 1) begin
            if(@endPos = 0)
                set @endPos = len(@value) + 1

            return substring(@value, @startPos, @endPos - @startPos)
        end

        set @position = @position - 1
    end

    return null
end

and the usage:

select dbo.GetStringPartByDelimeter ('a;b;c;d;e', ';', 3)

which returns:

c
Mustafa Ekici
  • 6,645
  • 8
  • 50
  • 70
  • I like this solution as an option to return a single substring as opposed to getting a parsed table that you then need to select from. Using a table result has its uses, but for what I needed this worked perfectly. – James H Mar 29 '16 at 21:16
7

If your database has compatibility level of 130 or higher then you can use the STRING_SPLIT function along with OFFSET FETCH clauses to get the specific item by index.

To get the item at index N (zero based), you can use the following code

SELECT value
FROM STRING_SPLIT('Hello John Smith',' ')
ORDER BY (SELECT NULL)
OFFSET N ROWS
FETCH NEXT 1 ROWS ONLY

To check the compatibility level of your database, execute this code:

SELECT compatibility_level  
FROM sys.databases WHERE name = 'YourDBName';
Gorgi Rankovski
  • 2,223
  • 1
  • 20
  • 30
  • The trick is in the OFFSET 1 ROWS, which will skip the first item and will return the second item. If your indexes are 0-based and @X is the variable holding the item index you want to fetch, you can sure do OFFSET @X ROWS – Gorgi Rankovski Apr 09 '18 at 08:35
  • Okay, did not use this before... Nice to know... I'd still prefer the `xml`-split based approach, as it allows to fetch the value type-safe and does not need a sub-query, but this is a good one. +1 from my side – Shnugo Apr 09 '18 at 08:40
  • 3
    the problem here is that STRING_SPLIT does not guarantee the order of the returned results. So your item 1 may or may not be my item 1. – user1443098 Apr 08 '19 at 13:49
  • @GorgiRankovski, Using `STRING_SPLIT` demands for v2016+. In this case it is much better to use `OPENJSON` or `JSON_VALUE`. You might want to [check my answer](https://stackoverflow.com/a/38275075/5089204) – Shnugo Jul 01 '19 at 08:08
6

I was looking for the solution on net and the below works for me. Ref.

And you call the function like this :

SELECT * FROM dbo.split('ram shyam hari gopal',' ')

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE FUNCTION [dbo].[Split](@String VARCHAR(8000), @Delimiter CHAR(1))       
RETURNS @temptable TABLE (items VARCHAR(8000))       
AS       
BEGIN       
    DECLARE @idx INT       
    DECLARE @slice VARCHAR(8000)        
    SELECT @idx = 1       
    IF len(@String)<1 OR @String IS NULL  RETURN       
    WHILE @idx!= 0       
    BEGIN       
        SET @idx = charindex(@Delimiter,@String)       
        IF @idx!=0       
            SET @slice = LEFT(@String,@idx - 1)       
        ELSE       
            SET @slice = @String       
        IF(len(@slice)>0)  
            INSERT INTO @temptable(Items) VALUES(@slice)       
        SET @String = RIGHT(@String,len(@String) - @idx)       
        IF len(@String) = 0 break       
    END   
    RETURN       
END
shA.t
  • 15,232
  • 5
  • 47
  • 95
kta
  • 17,024
  • 7
  • 58
  • 43
5

The following example uses a recursive CTE

Update 18.09.2013

CREATE FUNCTION dbo.SplitStrings_CTE(@List nvarchar(max), @Delimiter nvarchar(1))
RETURNS @returns TABLE (val nvarchar(max), [level] int, PRIMARY KEY CLUSTERED([level]))
AS
BEGIN
;WITH cte AS
 (
  SELECT SUBSTRING(@List, 0, CHARINDEX(@Delimiter,  @List + @Delimiter)) AS val,
         CAST(STUFF(@List + @Delimiter, 1, CHARINDEX(@Delimiter, @List + @Delimiter), '') AS nvarchar(max)) AS stval, 
         1 AS [level]
  UNION ALL
  SELECT SUBSTRING(stval, 0, CHARINDEX(@Delimiter, stval)),
         CAST(STUFF(stval, 1, CHARINDEX(@Delimiter, stval), '') AS nvarchar(max)),
         [level] + 1
  FROM cte
  WHERE stval != ''
  )
  INSERT @returns
  SELECT REPLACE(val, ' ','' ) AS val, [level]
  FROM cte
  WHERE val > ''
  RETURN
END

Demo on SQLFiddle

Aleksandr Fedorenko
  • 15,209
  • 6
  • 35
  • 42
5

Try this:

CREATE function [SplitWordList]
(
 @list varchar(8000)
)
returns @t table 
(
 Word varchar(50) not null,
 Position int identity(1,1) not null
)
as begin
  declare 
    @pos int,
    @lpos int,
    @item varchar(100),
    @ignore varchar(100),
    @dl int,
    @a1 int,
    @a2 int,
    @z1 int,
    @z2 int,
    @n1 int,
    @n2 int,
    @c varchar(1),
    @a smallint
  select 
    @a1 = ascii('a'),
    @a2 = ascii('A'),
    @z1 = ascii('z'),
    @z2 = ascii('Z'),
    @n1 = ascii('0'),
    @n2 = ascii('9')
  set @ignore = '''"'
  set @pos = 1
  set @dl = datalength(@list)
  set @lpos = 1
  set @item = ''
  while (@pos <= @dl) begin
    set @c = substring(@list, @pos, 1)
    if (@ignore not like '%' + @c + '%') begin
      set @a = ascii(@c)
      if ((@a >= @a1) and (@a <= @z1))  
        or ((@a >= @a2) and (@a <= @z2))
        or ((@a >= @n1) and (@a <= @n2))
      begin
        set @item = @item + @c
      end else if (@item > '') begin
        insert into @t values (@item)
        set @item = ''
      end
    end 
    set @pos = @pos + 1
  end
  if (@item > '') begin
    insert into @t values (@item)
  end
  return
end

Test it like this:

select * from SplitWordList('Hello John Smith')
Seibar
  • 63,705
  • 37
  • 85
  • 98
  • I've gone through it & it is perfectly like what I want! even I can also customize it for ignoring special characters that I choose! – Vikas Sep 15 '10 at 06:57
2


    Alter Function dbo.fn_Split
    (
    @Expression nvarchar(max),
    @Delimiter  nvarchar(20) = ',',
    @Qualifier  char(1) = Null
    )
    RETURNS @Results TABLE (id int IDENTITY(1,1), value nvarchar(max))
    AS
    BEGIN
       /* USAGE
            Select * From dbo.fn_Split('apple pear grape banana orange honeydew cantalope 3 2 1 4', ' ', Null)
            Select * From dbo.fn_Split('1,abc,"Doe, John",4', ',', '"')
            Select * From dbo.fn_Split('Hello 0,"&""&&&&', ',', '"')
       */

       -- Declare Variables
       DECLARE
          @X     xml,
          @Temp  nvarchar(max),
          @Temp2 nvarchar(max),
          @Start int,
          @End   int

       -- HTML Encode @Expression
       Select @Expression = (Select @Expression For XML Path(''))

       -- Find all occurences of @Delimiter within @Qualifier and replace with |||***|||
       While PATINDEX('%' + @Qualifier + '%', @Expression) > 0 AND Len(IsNull(@Qualifier, '')) > 0
       BEGIN
          Select
             -- Starting character position of @Qualifier
             @Start = PATINDEX('%' + @Qualifier + '%', @Expression),
             -- @Expression starting at the @Start position
             @Temp = SubString(@Expression, @Start + 1, LEN(@Expression)-@Start+1),
             -- Next position of @Qualifier within @Expression
             @End = PATINDEX('%' + @Qualifier + '%', @Temp) - 1,
             -- The part of Expression found between the @Qualifiers
             @Temp2 = Case When @End &LT 0 Then @Temp Else Left(@Temp, @End) End,
             -- New @Expression
             @Expression = REPLACE(@Expression,
                                   @Qualifier + @Temp2 + Case When @End &LT 0 Then '' Else @Qualifier End,
                                   Replace(@Temp2, @Delimiter, '|||***|||')
                           )
       END

       -- Replace all occurences of @Delimiter within @Expression with '&lt/fn_Split&gt&ltfn_Split&gt'
       -- And convert it to XML so we can select from it
       SET
          @X = Cast('&ltfn_Split&gt' +
                    Replace(@Expression, @Delimiter, '&lt/fn_Split&gt&ltfn_Split&gt') +
                    '&lt/fn_Split&gt' as xml)

       -- Insert into our returnable table replacing '|||***|||' back to @Delimiter
       INSERT @Results
       SELECT
          "Value" = LTRIM(RTrim(Replace(C.value('.', 'nvarchar(max)'), '|||***|||', @Delimiter)))
       FROM
          @X.nodes('fn_Split') as X(C)

       -- Return our temp table
       RETURN
    END

T-Rex
  • 849
  • 6
  • 3
2

You can split a string in SQL without needing a function:

DECLARE @bla varchar(MAX)
SET @bla = 'BED40DFC-F468-46DD-8017-00EF2FA3E4A4,64B59FC5-3F4D-4B0E-9A48-01F3D4F220B0,A611A108-97CA-42F3-A2E1-057165339719,E72D95EA-578F-45FC-88E5-075F66FD726C'

-- http://stackoverflow.com/questions/14712864/how-to-query-values-from-xml-nodes
SELECT 
    x.XmlCol.value('.', 'varchar(36)') AS val 
FROM 
(
    SELECT 
    CAST('<e>' + REPLACE(@bla, ',', '</e><e>') + '</e>' AS xml) AS RawXml
) AS b 
CROSS APPLY b.RawXml.nodes('e') x(XmlCol);

If you need to support arbitrary strings (with xml special characters)

DECLARE @bla NVARCHAR(MAX)
SET @bla = '<html>unsafe & safe Utf8CharsDon''tGetEncoded ÄöÜ - "Conex"<html>,Barnes & Noble,abc,def,ghi'

-- http://stackoverflow.com/questions/14712864/how-to-query-values-from-xml-nodes
SELECT 
    x.XmlCol.value('.', 'nvarchar(MAX)') AS val 
FROM 
(
    SELECT 
    CAST('<e>' + REPLACE((SELECT @bla FOR XML PATH('')), ',', '</e><e>') + '</e>' AS xml) AS RawXml
) AS b 
CROSS APPLY b.RawXml.nodes('e') x(XmlCol); 
Stefan Steiger
  • 68,404
  • 63
  • 337
  • 408
1

I know it's an old Question, but i think some one can benefit from my solution.

select 
SUBSTRING(column_name,1,CHARINDEX(' ',column_name,1)-1)
,SUBSTRING(SUBSTRING(column_name,CHARINDEX(' ',column_name,1)+1,LEN(column_name))
    ,1
    ,CHARINDEX(' ',SUBSTRING(column_name,CHARINDEX(' ',column_name,1)+1,LEN(column_name)),1)-1)
,SUBSTRING(SUBSTRING(column_name,CHARINDEX(' ',column_name,1)+1,LEN(column_name))
    ,CHARINDEX(' ',SUBSTRING(column_name,CHARINDEX(' ',column_name,1)+1,LEN(column_name)),1)+1
    ,LEN(column_name))
from table_name

SQL FIDDLE

Advantages:

  • It separates all the 3 sub-strings deliminator by ' '.
  • One must not use while loop, as it decreases the performance.
  • No need to Pivot as all the resultant sub-string will be displayed in one Row

Limitations:

  • One must know the total no. of spaces (sub-string).

Note: the solution can give sub-string up to to N.

To overcame the limitation we can use the following ref.

But again the above solution can't be use in a table (Actaully i wasn't able to use it).

Again i hope this solution can help some-one.

Update: In case of Records > 50000 it is not advisable to use LOOPS as it will degrade the Performance

Community
  • 1
  • 1
Prahalad Gaggar
  • 10,486
  • 16
  • 46
  • 66
1

Almost all the other answers are replacing the string being split which wastes CPU cycles and performs unnecessary memory allocations.

I cover a much better way to do a string split here: http://www.digitalruby.com/split-string-sql-server/

Here is the code:

SET NOCOUNT ON

-- You will want to change nvarchar(MAX) to nvarchar(50), varchar(50) or whatever matches exactly with the string column you will be searching against
DECLARE @SplitStringTable TABLE (Value nvarchar(MAX) NOT NULL)
DECLARE @StringToSplit nvarchar(MAX) = 'your|string|to|split|here'
DECLARE @SplitEndPos int
DECLARE @SplitValue nvarchar(MAX)
DECLARE @SplitDelim nvarchar(1) = '|'
DECLARE @SplitStartPos int = 1

SET @SplitEndPos = CHARINDEX(@SplitDelim, @StringToSplit, @SplitStartPos)

WHILE @SplitEndPos > 0
BEGIN
    SET @SplitValue = SUBSTRING(@StringToSplit, @SplitStartPos, (@SplitEndPos - @SplitStartPos))
    INSERT @SplitStringTable (Value) VALUES (@SplitValue)
    SET @SplitStartPos = @SplitEndPos + 1
    SET @SplitEndPos = CHARINDEX(@SplitDelim, @StringToSplit, @SplitStartPos)
END

SET @SplitValue = SUBSTRING(@StringToSplit, @SplitStartPos, 2147483647)
INSERT @SplitStringTable (Value) VALUES(@SplitValue)

SET NOCOUNT OFF

-- You can select or join with the values in @SplitStringTable at this point.
jjxtra
  • 18,300
  • 12
  • 85
  • 128
1

Pure set-based solution using TVF with recursive CTE. You can JOIN and APPLY this function to any dataset.

create function [dbo].[SplitStringToResultSet] (@value varchar(max), @separator char(1))
returns table
as return
with r as (
    select value, cast(null as varchar(max)) [x], -1 [no] from (select rtrim(cast(@value as varchar(max))) [value]) as j
    union all
    select right(value, len(value)-case charindex(@separator, value) when 0 then len(value) else charindex(@separator, value) end) [value]
    , left(r.[value], case charindex(@separator, r.value) when 0 then len(r.value) else abs(charindex(@separator, r.[value])-1) end ) [x]
    , [no] + 1 [no]
    from r where value > '')

select ltrim(x) [value], [no] [index] from r where x is not null;
go

Usage:

select *
from [dbo].[SplitStringToResultSet]('Hello John Smith', ' ')
where [index] = 1;

Result:

value   index
-------------
John    1
Andrey Morozov
  • 7,396
  • 5
  • 46
  • 71
0

Recursive CTE solution with server pain, test it

MS SQL Server 2008 Schema Setup:

create table Course( Courses varchar(100) );
insert into Course values ('Hello John Smith');

Query 1:

with cte as
   ( select 
        left( Courses, charindex( ' ' , Courses) ) as a_l,
        cast( substring( Courses, 
                         charindex( ' ' , Courses) + 1 , 
                         len(Courses ) ) + ' ' 
              as varchar(100) )  as a_r,
        Courses as a,
        0 as n
     from Course t
    union all
      select 
        left(a_r, charindex( ' ' , a_r) ) as a_l,
        substring( a_r, charindex( ' ' , a_r) + 1 , len(a_R ) ) as a_r,
        cte.a,
        cte.n + 1 as n
    from Course t inner join cte 
         on t.Courses = cte.a and len( a_r ) > 0

   )
select a_l, n from cte
--where N = 1

Results:

|    A_L | N |
|--------|---|
| Hello  | 0 |
|  John  | 1 |
| Smith  | 2 |
dani herrera
  • 39,746
  • 4
  • 87
  • 153
0

while similar to the xml based answer by josejuan, i found that processing the xml path only once, then pivoting was moderately more efficient:

select ID,
    [3] as PathProvidingID,
    [4] as PathProvider,
    [5] as ComponentProvidingID,
    [6] as ComponentProviding,
    [7] as InputRecievingID,
    [8] as InputRecieving,
    [9] as RowsPassed,
    [10] as InputRecieving2
    from
    (
    select id,message,d.* from sysssislog cross apply       ( 
          SELECT Item = y.i.value('(./text())[1]', 'varchar(200)'),
              row_number() over(order by y.i) as rn
          FROM 
          ( 
             SELECT x = CONVERT(XML, '<i>' + REPLACE(Message, ':', '</i><i>') + '</i>').query('.')
          ) AS a CROSS APPLY x.nodes('i') AS y(i)
       ) d
       WHERE event
       = 
       'OnPipelineRowsSent'
    ) as tokens 
    pivot 
    ( max(item) for [rn] in ([3],[4],[5],[6],[7],[8],[9],[10]) 
    ) as data

ran in 8:30

select id,
tokens.value('(/n[3])', 'varchar(100)')as PathProvidingID,
tokens.value('(/n[4])', 'varchar(100)') as PathProvider,
tokens.value('(/n[5])', 'varchar(100)') as ComponentProvidingID,
tokens.value('(/n[6])', 'varchar(100)') as ComponentProviding,
tokens.value('(/n[7])', 'varchar(100)') as InputRecievingID,
tokens.value('(/n[8])', 'varchar(100)') as InputRecieving,
tokens.value('(/n[9])', 'varchar(100)') as RowsPassed
 from
(
    select id, Convert(xml,'<n>'+Replace(message,'.','</n><n>')+'</n>') tokens
         from sysssislog 
       WHERE event
       = 
       'OnPipelineRowsSent'
    ) as data

ran in 9:20

Andrew Hill
  • 1,752
  • 1
  • 24
  • 29
0
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

AND USE IT

select *from dbo.fnSplitString('Querying SQL Server','')
Savas Adar
  • 3,357
  • 2
  • 38
  • 48
0

if anyone wants to get only one part of the seperatured text can use this

select * from fromSplitStringSep('Word1 wordr2 word3',' ')

CREATE function [dbo].[SplitStringSep] 
(
    @str nvarchar(4000), 
    @separator char(1)
)
returns table
AS
return (
    with tokens(p, a, b) AS (
        select 
        1, 
        1, 
        charindex(@separator, @str)
        union all
        select
            p + 1, 
            b + 1, 
            charindex(@separator, @str, b + 1)
        from tokens
        where b > 0
        )
        select
            p-1 zeroBasedOccurance,
            substring(
                @str, 
                a, 
                case when b > 0 then b-a ELSE 4000 end) 
            AS s
        from tokens
  )
Abhishek
  • 2,829
  • 3
  • 29
  • 56
nazim hatipoglu
  • 616
  • 11
  • 24
0

I devoloped this,

declare @x nvarchar(Max) = 'ali.veli.deli.';
declare @item nvarchar(Max);
declare @splitter char='.';

while CHARINDEX(@splitter,@x) != 0
begin
    set @item = LEFT(@x,CHARINDEX(@splitter,@x))
    set @x    = RIGHT(@x,len(@x)-len(@item) )
     select @item as item, @x as x;
end

the only attention you should is dot '.' that end of the @x is always should be there.

Ali CAKIL
  • 172
  • 2
  • 18
0
declare @strng varchar(max)='hello john smith'
select (
    substring(
        @strng,
        charindex(' ', @strng) + 1,
        (
          (charindex(' ', @strng, charindex(' ', @strng) + 1))
          - charindex(' ',@strng)
        )
    ))
Andre Figueiredo
  • 11,003
  • 6
  • 42
  • 68
Smart003
  • 1,057
  • 1
  • 14
  • 28
0

building on @NothingsImpossible solution, or, rather, comment on the most voted answer (just below the accepted one), i found the following quick-and-dirty solution fulfill my own needs - it has a benefit of being solely within SQL domain.

given a string "first;second;third;fourth;fifth", say, I want to get the third token. this works only if we know how many tokens the string is going to have - in this case it's 5. so my way of action is to chop the last two tokens away (inner query), and then to chop the first two tokens away (outer query)

i know that this is ugly and covers the specific conditions i was in, but am posting it just in case somebody finds it useful. cheers

select 
    REVERSE(
        SUBSTRING(
            reverse_substring, 
            0, 
            CHARINDEX(';', reverse_substring)
        )
    ) 
from 
(
    select 
        msg,
        SUBSTRING(
            REVERSE(msg), 
            CHARINDEX(
                ';', 
                REVERSE(msg), 
                CHARINDEX(
                    ';',
                    REVERSE(msg)
                )+1
            )+1,
            1000
        ) reverse_substring
    from 
    (
        select 'first;second;third;fourth;fifth' msg
    ) a
) b
hello_earth
  • 1,068
  • 1
  • 20
  • 31
  • *this works only if we know how many tokens the string is going to have* - a breaking limitation... – Shnugo Apr 09 '18 at 08:42
0

Starting with SQL Server 2016 we string_split

DECLARE @string varchar(100) = 'Richard, Mike, Mark'

SELECT value FROM string_split(@string, ',')
Victor Hugo Terceros
  • 2,438
  • 2
  • 13
  • 26
  • This is well and good, but it doesn't address the question of getting the nth result. – Johnie Karr Dec 28 '17 at 13:47
  • `STRING_SPLIT` does not guarantee to return the same order. But `OPENJSON` does (see my [answer (update section)](https://stackoverflow.com/a/38275075/5089204)) – Shnugo Sep 14 '18 at 07:05
0

A modern approach using STRING_SPLIT, requires SQL Server 2016 and above.

DECLARE @string varchar(100) = 'Hello John Smith'

SELECT
    ROW_NUMBER() OVER (ORDER BY value) AS RowNr,
    value
FROM string_split(@string, ' ')

Result:

RowNr   value
1       Hello
2       John
3       Smith

Now it is possible to get th nth element from the row number.

uzr
  • 1,102
  • 1
  • 12
  • 24
  • 2
    `STRING_SPLIT` does not guarantee to return the same order. But `OPENJSON` does (see my [answer (update section)](https://stackoverflow.com/a/38275075/5089204)) – Shnugo Sep 14 '18 at 07:04
0

Aaron Bertrand's answer is great, but flawed. It doesn't accurately handle a space as a delimiter (as was the example in the original question) since the length function strips trailing spaces.

The following is his code, with a small adjustment to allow for a space delimiter:

CREATE FUNCTION [dbo].[SplitString]
(
    @List NVARCHAR(MAX),
    @Delim VARCHAR(255)
)
RETURNS TABLE
AS
    RETURN ( SELECT [Value] FROM 
      ( 
        SELECT 
          [Value] = LTRIM(RTRIM(SUBSTRING(@List, [Number],
          CHARINDEX(@Delim, @List + @Delim, [Number]) - [Number])))
        FROM (SELECT Number = ROW_NUMBER() OVER (ORDER BY name)
          FROM sys.all_objects) AS x
          WHERE Number <= LEN(@List)
          AND SUBSTRING(@Delim + @List, [Number], LEN(@Delim+'x')-1) = @Delim
      ) AS y
    );
Derek Brown
  • 3,867
  • 4
  • 22
  • 40
zipppy
  • 49
  • 3
0

Here is a function that will accomplish the question's goal of splitting a string and accessing item X:

CREATE FUNCTION [dbo].[SplitString]
(
   @List       VARCHAR(MAX),
   @Delimiter  VARCHAR(255),
   @ElementNumber INT
)
RETURNS VARCHAR(MAX)
AS
BEGIN

       DECLARE @inp VARCHAR(MAX)
       SET @inp = (SELECT REPLACE(@List,@Delimiter,'_DELMTR_') FOR XML PATH(''))

       DECLARE @xml XML
       SET @xml = '<split><el>' + REPLACE(@inp,'_DELMTR_','</el><el>') + '</el></split>'

       DECLARE @ret VARCHAR(MAX)
       SET @ret = (SELECT
              el = split.el.value('.','varchar(max)')
       FROM  @xml.nodes('/split/el[string-length(.)>0][position() = sql:variable("@elementnumber")]') split(el))

       RETURN @ret

END

Usage:

SELECT dbo.SplitString('Hello John Smith', ' ', 2)

Result:

John
VinceL
  • 100
  • 1
  • 2
  • 9
  • This is to complicated... No need for `.nodes()`. You can place the `XQuery` into `.value()` directly (see my answer). Btw: Scalar funcitons are very bad performers. Much better was an *inline TVF*, even if it returns just one cell in one row... – Shnugo Jun 29 '18 at 09:32
0

SIMPLE SOLUTION FOR PARSING FIRST AND LAST NAME

DECLARE @Name varchar(10) = 'John Smith'

-- Get First Name
SELECT SUBSTRING(@Name, 0, (SELECT CHARINDEX(' ', @Name)))

-- Get Last Name
SELECT SUBSTRING(@Name, (SELECT CHARINDEX(' ', @Name)) + 1, LEN(@Name))

In my case (and in many others it seems...), I have a list of first and last names separated by a single space. This can be used directly inside a select statement to parse first and last name.

-- i.e. Get First and Last Name from a table of Full Names
SELECT SUBSTRING(FullName, 0, (SELECT CHARINDEX(' ', FullName))) as FirstName,
SUBSTRING(FullName, (SELECT CHARINDEX(' ', FullName)) + 1, LEN(FullName)) as LastName,
From FullNameTable
Sam K
  • 289
  • 1
  • 6
  • 11
0

I know its late, but I recently had this requirement and came up with the below code. I don't have a choice to use User defined function. Hope this helps.

SELECT 
    SUBSTRING(
                SUBSTRING('Hello John Smith' ,0,CHARINDEX(' ','Hello John Smith',CHARINDEX(' ','Hello John Smith')+1)
                        ),CHARINDEX(' ','Hello John Smith'),LEN('Hello John Smith')
            )
Nick
  • 118,076
  • 20
  • 42
  • 73
GGadde
  • 395
  • 1
  • 14
0
CREATE TABLE test(
    id int,
    adress varchar(100)
);
INSERT INTO test VALUES(1, 'Ludovic Aubert, 42 rue de la Victoire, 75009, Paris, France'),(2, 'Jose Garcia, 1 Calle de la Victoria, 56500 Barcelona, Espana');

SELECT id, value, COUNT(*) OVER (PARTITION BY id) AS n, ROW_NUMBER() OVER (PARTITION BY id ORDER BY (SELECT NULL)) AS rn, adress
FROM test
CROSS APPLY STRING_SPLIT(adress, ',')

enter image description here

Ludovic Aubert
  • 7,038
  • 3
  • 13
  • 17
-1

Well, mine isn't all that simpler, but here is the code I use to split a comma-delimited input variable into individual values, and put it into a table variable. I'm sure you could modify this slightly to split based on a space and then to do a basic SELECT query against that table variable to get your results.

-- Create temporary table to parse the list of accounting cycles.
DECLARE @tblAccountingCycles table
(
    AccountingCycle varchar(10)
)

DECLARE @vchAccountingCycle varchar(10)
DECLARE @intPosition int

SET @vchAccountingCycleIDs = LTRIM(RTRIM(@vchAccountingCycleIDs)) + ','
SET @intPosition = CHARINDEX(',', @vchAccountingCycleIDs, 1)

IF REPLACE(@vchAccountingCycleIDs, ',', '') <> ''
BEGIN
    WHILE @intPosition > 0
    BEGIN
        SET @vchAccountingCycle = LTRIM(RTRIM(LEFT(@vchAccountingCycleIDs, @intPosition - 1)))
        IF @vchAccountingCycle <> ''
        BEGIN
            INSERT INTO @tblAccountingCycles (AccountingCycle) VALUES (@vchAccountingCycle)
        END
        SET @vchAccountingCycleIDs = RIGHT(@vchAccountingCycleIDs, LEN(@vchAccountingCycleIDs) - @intPosition)
        SET @intPosition = CHARINDEX(',', @vchAccountingCycleIDs, 1)
    END
END

The concept is pretty much the same. One other alternative is to leverage the .NET compatibility within SQL Server 2005 itself. You can essentially write yourself a simple method in .NET that would split the string and then expose that as a stored procedure/function.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Dillie-O
  • 28,071
  • 14
  • 93
  • 139
  • an example of doing it in .NET (CLR Procedures/functions) can be found here http://www.cstruter.com/blog/260 – cstruter Jan 28 '11 at 05:47
-1

I realize this is a really old question, but starting with SQL Server 2016 there are functions for parsing JSON data that can be used to specifically address the OP's question--and without splitting strings or resorting to a user-defined function. To access an item at a particular index of a delimited string, use the JSON_VALUE function. Properly formatted JSON data is required, however: strings must be enclosed in double quotes " and the delimiter must be a comma ,, with the entire string enclosed in square brackets [].

DECLARE @SampleString NVARCHAR(MAX) = '"Hello John Smith"';
--Format as JSON data.
SET @SampleString = '[' + REPLACE(@SampleString, ' ', '","') + ']';
SELECT 
    JSON_VALUE(@SampleString, '$[0]') AS Element1Value,
    JSON_VALUE(@SampleString, '$[1]') AS Element2Value,
    JSON_VALUE(@SampleString, '$[2]') AS Element3Value;

Output

Element1Value         Element2Value       Element3Value
--------------------- ------------------- ------------------------------
Hello                 John                Smith

(1 row affected)
Dave Mason
  • 4,213
  • 1
  • 20
  • 24
-1

Using SQL Server 2016 and above. Use this code to TRIM strings, ignore NULL values and apply a row index in the correct order. It also works with a space delimiter:

DECLARE @STRING_VALUE NVARCHAR(MAX) = 'one, two,,three, four,     five'

SELECT ROW_NUMBER() OVER (ORDER BY R.[index]) [index], R.[value] FROM
(
    SELECT
        1 [index], NULLIF(TRIM([value]), '') [value] FROM STRING_SPLIT(@STRING_VALUE, ',') T
    WHERE
        NULLIF(TRIM([value]), '') IS NOT NULL
) R
GBGOLC
  • 430
  • 7
  • 6
-1

If you check the following SQL tutorial on splitting string using SQL, you will find a number of functions that can be used to split a given string on SQL Server

For example, SplitAndReturnNth UDF function can be used to split a text using a separator and return the Nth piece as the output of the function

select dbo.SplitAndReturnNth('Hello John Smith',' ',2)

enter image description here

Eralper
  • 6,081
  • 2
  • 17
  • 27
-2

Here's my solution that may help someone. Modification of Jonesinator's answer above.

If I have a string of delimited INT values and want a table of INTs returned (Which I can then join on). e.g. '1,20,3,343,44,6,8765'

Create a UDF:

IF OBJECT_ID(N'dbo.ufn_GetIntTableFromDelimitedList', N'TF') IS NOT NULL
    DROP FUNCTION dbo.[ufn_GetIntTableFromDelimitedList];
GO

CREATE FUNCTION dbo.[ufn_GetIntTableFromDelimitedList](@String NVARCHAR(MAX),                 @Delimiter CHAR(1))

RETURNS @table TABLE 
(
    Value INT NOT NULL
)
AS 
BEGIN
DECLARE @Pattern NVARCHAR(3)
SET @Pattern = '%' + @Delimiter + '%'
DECLARE @Value NVARCHAR(MAX)

WHILE LEN(@String) > 0
    BEGIN
        IF PATINDEX(@Pattern, @String) > 0
        BEGIN
            SET @Value = SUBSTRING(@String, 0, PATINDEX(@Pattern, @String))
            INSERT INTO @table (Value) VALUES (@Value)

            SET @String = SUBSTRING(@String, LEN(@Value + @Delimiter) + 1, LEN(@String))
        END
        ELSE
        BEGIN
            -- Just the one value.
            INSERT INTO @table (Value) VALUES (@String)
            RETURN
        END
    END

RETURN
END
GO

Then get the table results:

SELECT * FROM dbo.[ufn_GetIntTableFromDelimitedList]('1,20,3,343,44,6,8765', ',')

1
20
3
343
44
6
8765

And in a join statement:

SELECT [ID], [FirstName]
FROM [User] u
JOIN dbo.[ufn_GetIntTableFromDelimitedList]('1,20,3,343,44,6,8765', ',') t ON u.[ID] = t.[Value]

1    Elvis
20   Karen
3    David
343  Simon
44   Raj
6    Mike
8765 Richard

If you want to return a list of NVARCHARs instead of INTs then just change the table definition:

RETURNS @table TABLE 
(
    Value NVARCHAR(MAX) NOT NULL
)
mkaj
  • 3,251
  • 1
  • 27
  • 23
-2

Here is a SQL UDF that can split a string and grab just a certain piece.

create FUNCTION [dbo].[udf_SplitParseOut]
(
    @List nvarchar(MAX),
    @SplitOn nvarchar(5),
    @GetIndex smallint
)  
returns varchar(1000)
AS  

BEGIN

DECLARE @RtnValue table 
(

    Id int identity(0,1),
    Value nvarchar(MAX)
) 


    DECLARE @result varchar(1000)

    While (Charindex(@SplitOn,@List)>0)
    Begin
        Insert Into @RtnValue (value)
        Select Value = ltrim(rtrim(Substring(@List,1,Charindex(@SplitOn,@List)-1)))
        Set @List = Substring(@List,Charindex(@SplitOn,@List)+len(@SplitOn),len(@List))
    End

    Insert Into @RtnValue (Value)
    Select Value = ltrim(rtrim(@List))

    select @result = value from @RtnValue where ID = @GetIndex

    Return @result
END
SparkeyG
  • 532
  • 9
  • 22
Matt Watson
  • 940
  • 7
  • 9
-2

A simple optimized algorithm :

ALTER FUNCTION [dbo].[Split]( @Text NVARCHAR(200),@Splitor CHAR(1) )
RETURNS @Result TABLE ( value NVARCHAR(50)) 
AS
BEGIN
    DECLARE @PathInd INT
    Set @Text+=@Splitor
    WHILE LEN(@Text) > 0
    BEGIN
        SET @PathInd=PATINDEX('%'+@Splitor+'%',@Text)
        INSERT INTO  @Result VALUES(SUBSTRING(@Text, 0, @PathInd))
        SET @Text= SUBSTRING(@Text, @PathInd+1, LEN(@Text))
    END
        RETURN 
END
Mohsen
  • 3,224
  • 7
  • 32
  • 62
-2

I've been using vzczc's answer using recursive cte's for some time, but have wanted to update it to handle a variable length separator and also to handle strings with leading and lagging "separators" such as when you have a csv file with records such as:

"Bob","Smith","Sunnyvale","CA"

or when you are dealing with six part fqn's as shown below. I use these extensively for logging of the subject_fqn for auditing, error handling, etc. and parsename only handles four parts:

[netbios_name].[machine_name].[instance].[database].[schema].[table].[column]

Here is my updated version, and thanks to vzczc's for his original post!

select * from [utility].[split_string](N'"this"."string"."gets"."split"."and"."removes"."leading"."and"."trailing"."quotes"', N'"."', N'"', N'"');

select * from [utility].[split_string](N'"this"."string"."gets"."split"."but"."leaves"."leading"."and"."trailing"."quotes"', N'"."', null, null);

select * from [utility].[split_string](N'[netbios_name].[machine_name].[instance].[database].[schema].[table].[column]', N'].[', N'[', N']');

create function [utility].[split_string] ( 
  @input       [nvarchar](max) 
  , @separator [sysname] 
  , @lead      [sysname] 
  , @lag       [sysname]) 
returns @node_list table ( 
  [index]  [int] 
  , [node] [nvarchar](max)) 
  begin 
      declare @separator_length [int]= len(@separator) 
              , @lead_length    [int] = isnull(len(@lead), 0) 
              , @lag_length     [int] = isnull(len(@lag), 0); 
      -- 
      set @input = right(@input, len(@input) - @lead_length); 
      set @input = left(@input, len(@input) - @lag_length); 
      -- 
      with [splitter]([index], [starting_position], [start_location]) 
           as (select cast(@separator_length as [bigint]) 
                      , cast(1 as [bigint]) 
                      , charindex(@separator, @input) 
               union all 
               select [index] + 1 
                      , [start_location] + @separator_length 
                      , charindex(@separator, @input, [start_location] + @separator_length) 
               from   [splitter] 
               where  [start_location] > 0) 
      -- 
      insert into @node_list 
                  ([index],[node]) 
        select [index] - @separator_length                   as [index] 
               , substring(@input, [starting_position], case 
                                                            when [start_location] > 0 
                                                                then 
                                                              [start_location] - [starting_position] 
                                                            else 
                                                              len(@input) 
                                                        end) as [node] 
        from   [splitter]; 
      -- 
      return; 
  end; 
go 
Nadeem_MK
  • 7,031
  • 7
  • 44
  • 56