2008-08-05 18:15:47 8 Comments
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"?
Related Questions
Sponsored Content
28 Answered Questions
[SOLVED] How can I prevent SQL injection in PHP?
- 2008-09-12 23:55:00
- Andrew G. Johnson
- 1528844 View
- 2779 Score
- 28 Answer
- Tags: php mysql sql security sql-injection
76 Answered Questions
20 Answered Questions
[SOLVED] Get size of all tables in database
- 2011-10-25 16:14:34
- Eric
- 1048345 View
- 1005 Score
- 20 Answer
- Tags: sql-server tsql
32 Answered Questions
[SOLVED] How do I UPDATE from a SELECT in SQL Server?
- 2010-02-25 14:36:53
- jamesmhaley
- 3684889 View
- 3241 Score
- 32 Answer
- Tags: sql sql-server tsql select
38 Answered Questions
[SOLVED] How to return only the Date from a SQL Server DateTime datatype
- 2008-09-22 03:31:33
- eddiegroves
- 2432826 View
- 1550 Score
- 38 Answer
- Tags: sql sql-server tsql date datetime
57 Answered Questions
24 Answered Questions
[SOLVED] How to check if a column exists in a SQL Server table?
- 2008-09-25 12:34:00
- Maciej
- 1058603 View
- 1674 Score
- 24 Answer
- Tags: sql-server sql-server-2008 tsql sql-server-2012 sql-server-2016
37 Answered Questions
[SOLVED] How can I remove duplicate rows?
- 2008-08-20 21:51:29
- Seibar
- 1087771 View
- 1197 Score
- 37 Answer
- Tags: sql-server tsql duplicates
9 Answered Questions
[SOLVED] SQL update query using joins
- 2009-06-11 18:49:33
- Shyju
- 768766 View
- 566 Score
- 9 Answer
- Tags: sql sql-server tsql sql-server-2005 sql-update
14 Answered Questions
[SOLVED] DateTime2 vs DateTime in SQL Server
- 2009-08-26 11:45:10
- Mikeon
- 416736 View
- 668 Score
- 14 Answer
- Tags: sql sql-server tsql datetime datetime2
30 comments
@Salman A 2019-01-27 15:27:58
If the substrings do not contain duplicates then you can use the following:
The
STRING_SPLIT
generates the substrings but does not provide index of the substring. You can useCHARINDEX
to generate the index number and it will be correct as long as the substrings are unique. It will fail fora b b c
,a b c c d e
, etc.@Aaron Bertrand 2013-11-12 17:16:08
Most of the solutions here use while loops or recursive CTEs. A set-based approach will be superior, I promise:
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()
andSTRING_AGG()
:@T-moty 2015-10-21 15:01:40
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?
@Mikhail Boyarsky 2016-09-23 13:20:50
Thanks to Aaron; But can someone explain why if I pass
varchar
(notvarchar(max)
) as the argument this function returns empty list? likedeclare @list varchar = 'something'; select from dbo.SplitString(@list, ';');
@Aaron Bertrand 2016-09-23 20:08:29
@Mikhail Because
varchar
without length can either bevarchar(30)
orvarchar(1)
depending on context. Don't try to understand that problem - just don't use that syntax. Ever.@wwmbes 2016-10-11 10:27:13
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@Aaron Bertrand 2016-10-11 15:07:25
@wwmbes Try using a delimiter other than space. Many functions are going to have the issue that trailing spaces are dropped.
@wwmbes 2016-10-26 12:00:10
@AaronBertrand The original problem posted by GateKiller involves a space delimiter.
@GGadde 2018-09-17 21:07:09
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.
@Shnugo 2016-07-08 20:41:19
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,
CTE
s, multipleCHARINDEX
,REVERSE
andPATINDEX
, inventing functions, call for CLR methods, number tables,CROSS APPLY
s ... 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:
Of course you can use variables for delimiter and position (use
sql:column
to retrieve the position directly from a query's value):If your string might include forbidden characters (especially one among
&><
), you still can do it this way. Just useFOR 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:
UPDATE for SQL-Server 2016+
Regretfully the developers forgot to return the part's index with
STRING_SPLIT
. But, using SQL-Server 2016+, there isOPENJSON
.The documentation states clearly:
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:
@Salman A 2019-01-28 07:26:13
Re: if your string might include forbidden characters... you could simply wrap the substrings like so
<x><![CDATA[x<&>x]]></x>
.@Shnugo 2019-01-28 07:32:30
@SalmanA, yeah ,
CDATA
-sections can deal with this too... But after the cast they are gone (changed to escapedtext()
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).@Sam K 2018-08-20 18:59:23
SIMPLE SOLUTION FOR PARSING FIRST AND LAST 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.
@Gorgi Rankovski 2018-04-05 10:23:24
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
To check the compatibility level of your database, execute this code:
@Shnugo 2018-04-09 07:23:41
This is good for item 1, but rather clumsy when you want to access item x. I really do not undestand, why this function was implemented without a
Position
column.@Gorgi Rankovski 2018-04-09 08:35:49
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
@Shnugo 2018-04-09 08:40:37
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@VinceL 2018-04-26 21:16:34
Here is a function that will accomplish the question's goal of splitting a string and accessing item X:
Usage:
Result:
@Shnugo 2018-06-29 09:32:26
This is to complicated... No need for
.nodes()
. You can place theXQuery
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...@zipppy 2018-03-22 14:38:24
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:
@uzr 2018-01-02 15:02:56
A modern approach using STRING_SPLIT, requires SQL Server 2016 and above.
Result:
Now it is possible to get th nth element from the row number.
@Shnugo 2018-09-14 07:04:51
STRING_SPLIT
does not guarantee to return the same order. ButOPENJSON
does (see my answer (update section))@Victor Hugo Terceros 2017-09-04 21:52:57
Starting with SQL Server 2016 we string_split
@Johnie Karr 2017-12-28 13:47:29
This is well and good, but it doesn't address the question of getting the nth result.
@Shnugo 2018-09-14 07:05:02
STRING_SPLIT
does not guarantee to return the same order. ButOPENJSON
does (see my answer (update section))@Smart003 2016-07-14 05:29:41
@Jonesinator 2008-08-05 18:28:38
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:
@Beth 2010-09-29 15:13:55
why
SET @p_SourceText = RTRIM( LTRIM( @p_SourceText)) SET @w_Length = DATALENGTH( RTRIM( LTRIM( @p_SourceText)))
and notSET @p_SourceText = RTRIM( LTRIM( @p_SourceText)) SET @w_Length = DATALENGTH( @p_SourceText)
?@Filip De Vos 2011-03-18 13:55:40
@GateKiller This solution does not support Unicode & it uses hard coded numeric(18,3) which doesn't make it a viable "reusable" function.
@jjxtra 2015-05-26 16:56:29
This works but allocates a lot of memory and wastes CPU.
@Ramazan Binarbasi 2016-01-08 15:02:17
I've added a new answer, which I guess is the fastest..
@qJake 2017-04-03 20:24:41
As of SQL Server 2016, there is now a built-in function
STRING_SPLIT
that will split a string and return a one-column table result which you can use in aSELECT
statement or elsewhere.@Brandon Griffin 2017-05-02 19:38: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.
@Muhammad Ashikuzzaman 2018-11-19 16:06:15
@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
@hello_earth 2016-10-31 14:18:19
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
@Shnugo 2018-04-09 08:42:50
this works only if we know how many tokens the string is going to have - a breaking limitation...
@Nathan Skerl 2008-10-27 16:48:51
You can leverage a Number table to do the string parsing.
Create a physical numbers table:
Create test table with 1000000 rows
Create the function
Usage (outputs 3mil rows in 40s on my laptop)
cleanup
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.
@Pking 2012-12-06 13:01:09
The best solution IMO, the others have some kind of limitation.. this is fast and can parse long strings with many elements.
@hatchet 2014-10-28 16:13:14
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?@Nathan Skerl 2014-10-28 17:43:26
Agreed, would be more intuitive in the asc direction. I was following parsename() convention which uses desc
@Tim Abell 2016-06-24 09:46:18
some explanation as to how this works would be great
@wwmbes 2016-10-28 07:41:32
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 2016-10-28 16:23: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?
@Nathan Skerl 2016-10-28 17:49:10
@wwmbes Try using a physical number table with a clustered index. The usage of master..spt_values is just for illustration
@Nathan Skerl 2016-10-28 18:06:43
@wwmbes added a physical number table example
@vzczc 2008-08-05 18:57:03
First, create a function (using CTE, common table expression does away with the need for a temp table)
Then, use it as any table (or modify it to fit within your existing stored proc) like this.
Update
Previous version would fail for input string longer than 4000 chars. This version takes care of the limitation:
Usage remains the same.
@Pking 2012-11-07 15:31:18
It's elegant but only works for 100 elements because of the limit of recursion depth.
@Michał Powaga 2013-03-14 14:45:06
@Pking, no, the default is
100
(to prevent infinite loop). Use MAXRECURSION hint to define number of recursion levels (0
to32767
,0
is "no limit" - may crush server). BTW, much better answer thanPARSENAME
, because it's universal :-). +1@Michał Powaga 2013-03-15 09:03:46
Adding
maxrecursion
to this solution keep in mind this question and its answers How to setup themaxrecursion
option for a CTE inside a Table-Valued-Function.@AHiggins 2015-07-30 18:05:52
Specifically, reference the answer by Crisfole - his method slows it somewhat, but is simpler than most other options.
@Tim Abell 2016-06-24 09:45:06
minor point but the usage doesn't remain the same because you changed the column name, so
s
is no longer defined@Ramazan Binarbasi 2016-01-08 14:30:11
Yet another get n'th part of string by delimeter function:
and the usage:
which returns:
@James H 2016-03-29 21:16:14
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.
@Nathan Bedford 2008-08-05 18:45:19
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:
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).
Obvious problem is when the string already contains a period. I still think using a UDF is the best way...any other suggestions?
@Nathan Bedford 2009-07-01 15:54:58
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.
@Factor Mystic 2010-07-12 14:09:43
This is a great hack, and also makes me weep that something like this is necessary for something so friggin simple in real languages.
@NothingsImpossible 2012-05-14 13:57:58
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
@Bacon Bits 2014-12-08 04:11:25
@FactorMystic 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.@MrBliz 2015-06-25 11:57:04
This is uber cool. It just so happens that the data i'm working with has four elements
@hello_earth 2016-02-08 09:09:32
+1 can always concoct a quick and dirty working solution for >4 tokens with a combination of reverse(), charindex(), substring()
@Tim Abell 2016-06-24 09:34:23
@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.
@wwmbes 2016-10-27 17:14:16
When the number of splits in the string varies from left to right, then the @NothingsImpossible route is the way to go .
@wwmbes 2016-10-28 07:23:04
@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 2016-10-28 07:29:06
@hello_earth I'd love to see what you come up with. Please show us.
@hello_earth 2016-10-31 14:20:19
@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
@Kyle - Microsoft 2017-05-30 20:31:46
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.
@aKiRa 2018-05-24 13:22:12
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. docs.microsoft.com/it-it/sql/t-sql/functions/…
@Stefan Steiger 2015-10-23 10:07:55
You can split a string in SQL without needing a function:
If you need to support arbitrary strings (with xml special characters)
@Ali CAKIL 2015-10-15 10:50:09
I devoloped this,
the only attention you should is dot '.' that end of the @x is always should be there.
@nazim hatipoglu 2015-02-13 09:14:22
if anyone wants to get only one part of the seperatured text can use this
select * from fromSplitStringSep('Word1 wordr2 word3',' ')
@angel 2013-08-13 15:11:27
I use the answer of frederic but this did not work in SQL Server 2005
I modified it and I'm using
select
withunion all
and it worksAnd the result-set is:
@Abdurrahman I. 2016-03-24 08:36:37
This is really great i've ever seen in sql stuff, it worked for my job and i appreciate that, thanks !
@Kristen Hammack 2016-08-10 13:20:44
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.@kta 2011-11-20 06:40:26
I was looking for the solution on net and the below works for me. Ref.
And you call the function like this :
@Björn Lindqvist 2014-10-03 09:21:53
You can't easily access the Nth item using this function.
@Frederic 2013-03-01 16:26:59
What about using
string
andvalues()
statement?Result-set achieved.
@angel 2013-08-13 15:06:08
i used your answer but did not work, but i modified and this worked with union all, i am using sql 2005
@jjxtra 2014-08-26 16:50:04
Almost all the other answers split code 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:
@Andrey Morozov 2015-01-13 06:37:07
Pure set-based solution using
TVF
with recursiveCTE
. You canJOIN
andAPPLY
this function to any dataset.Usage:
Result:
@Savas Adar 2014-12-20 11:58:36
AND USE IT
@Andrew Hill 2014-12-08 03:59:24
while similar to the xml based answer by josejuan, i found that processing the xml path only once, then pivoting was moderately more efficient:
ran in 8:30
ran in 9:20
@josejuan 2014-11-11 14:31:37
This pattern works fine and you can generalize
note FIELD, INDEX and TYPE.
Let some table with identifiers like
Then, you can write
splitting and casting all parts.
@Andrew Hill 2014-12-08 03:53:53
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)
@Katherine Elizabeth Lightsey 2014-08-19 19:45:33
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:
Here is my updated version, and thanks to vzczc's for his original post!
@Mohsen 2014-05-01 06:26:39
A simple optimized algorithm :