Click here to monitor SSC
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)

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

/* --- COPY/PAST YOUR CASE STATEMENT --- */

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) = ','

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

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

Extracting Rows from Delimited Strings

Extracting Rows from Delimited Strings

SCRIPT

Delimited String Parsing Functions - Big Set

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

SCRIPT

Delimited String Parser

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

SCRIPT

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...

SCRIPT

Get String between two delimiters

Get string between two delimiters

Tags
concatenate    
function    
split    
string    
t-sql    
 
Contribute