Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

Simple and efficient way to split string with seperator

By Steven Rao,

   I have seen too many people write function to split string with loop. This maybe not efficient, especially if you want to apply the function with large tables.
   The function fn_SplitString here is very simple and it is also very efficient. It does not use any looping. So we can apply it to a large table with very good performance. 
   I had few very good experience using similar idea to parse address string with large tables in the past.
    It uses an internal table master..spt_values from SQL server, which is avaliable for all version of SQL server. This table is also available from Sybase. So this same function and same idea will work with Sybase too.
    Currently the function remove the potential empty space at end of begin of the string value, but you may choose not to do that.
     If you prefer not to use internal system table, you can use other ways to generate a table with list of numbers.
     
     The function is easy to use, for example:
     
     select stringNumber, stringValue from fn_SplitString('split;my;string;with;a;separator',';')

       This is what you get as the result:

Total article views: 1491 | Views in the last 30 days: 11
 
Related Articles
FORUM

string comparison

function to compare two strings

BLOG

CONCAT() string function–SQL Server 2012

As the name indicates, CONCAT() will concatenate two or more strings. This is one of the two string ...

FORUM

"function"

"function"

FORUM

String functions

Join int value to a string

FORUM

Reverse string without built in functions

reverse string

Tags
string splitter    
t-sql    
 
Contribute

Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones
Editor, SQLServerCentral.com

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones