Using SQL Server 2005, how do I split a string so I can access item x?
For example, take the string "Hello John Smith". How can I split the string by a space and access the item at index 1 which should return "John"?
Using SQL Server 2005, how do I split a string so I can access item x? For example, take the string "Hello John Smith". How can I split the string by a space and access the item at index 1 which should return "John"? |
|||||
|
You may find the solution in SQL User Defined Function to Parse a Delimited String helpful (from The Code Project). U can use this simple logic -
|
|||||||
|
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 it's 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? |
|||||||||||||||||||||
|
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. |
|||||||||||||
|
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.
You would call it like this:
Edit: Updated solution to handle delimters with a len>1 as in :
|
|||||||||||||
|
No code, but read the definitive article on this. All solutions in other answers are flavours of the ones listed in this article: Arrays and Lists in SQL Server 2005 and Beyond Personally, I've used a Numbers table solution most often because it suits what I have to do... |
|||
|
You can leverage a Number table to do the string parsing lightning fast:
Usage:
|
||||
|
In my opinion you guys are making it way too complicated. Just create a CLR UDF and be done with it.
|
|||||
|
Here I post a simple way of solution
|
||||
|
Try this:
Test it like this:
|
|||
|
Option with recursive CTE
Demo on SQLFiddle |
||||
|
I know it's an old Question, but i think some one can benefit from my solution.
Advantages:
Limitations:
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 |
||||
|
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',' ')
|
||||
|
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.
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. |
||||
|
what about using string and 'values()' statement?
resultset achieved..
|
|||
|
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:
Then get the table results:
And in a join statement:
If you want to return a list of NVARCHARs instead of INTs then just change the table definition:
|
|||
|
This question is protected to prevent "thanks!", "me too!", or spam answers by new users. To answer it, you must have earned at least 10 reputation on this site.