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) = ','
@myString AS inputString
,[dbo].[Split_and_concatenate](@myString, @delimiter) AS outputString