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)

Split and concatenate function

By Tomaz Kastrun,

Function has two input parameters; a string and delimiter.

Parameter string will be used in function for:

1) to split the values

2) add any additional logic as denoted in part with


3) concatenating new values back to string

4) returning string.

Parameter delimiter is simply a char, used in function to destinguish how to split a string.

E.g.: '1,3,5,7,2,1' uses comma for delimitation between the values.

Alter function and taylor it to your needs in section marked with START and END. It uses CASE statement, but can easly be replaced with any other statement. Also joins can be added for bounding on to your data.

Usage for function is as following:
DECLARE @myString VARCHAR(10) = '1,1,2,2,42'
DECLARE @delimiter CHAR(1) = ','

     @myString AS inputString
    ,[dbo].[Split_and_concatenate](@myString, @delimiter) AS outputString

Total article views: 1721 | Views in the last 30 days: 6
Related Articles

Extracting Rows from Delimited Strings

Extracting Rows from Delimited Strings


Delimited String Parsing Functions - Big Set

Feed it large strings of delimited horizontal data and it returns it back as a vertical table.


Delimited String Parser

Parses delimited string into a table of up to 9 varchar fields.


Delimited String Parsing Functions - Big2D set

Feed it large strings of double-delimited horizontal data and it returns it back as a non-pivoted ve...


Get String between two delimiters

Get string between two delimiters


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

Already a member? Jump in:

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