SQL Clone
SQLServerCentral is supported by Redgate
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: 1591 | Views in the last 30 days: 2
Related Articles

Split String Function

This In-line Table Valued Function returns a table with individual records parsed from a string.


Function for String Occurences Count

This function is used to get the particular string occurences count from the given input string.


string comparison

function to compare two strings


CONCAT() string function–SQL Server 2012

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


SQL Server – String_Escape function in SQL Server 2016

Hi friends, in SQL server 2016 another string function that introduced is String_Escape. This functi...

string splitter