how to split and concatenate in sql server?

sql, sql-server, t-sql

Solution

declare @groups nvarchar(1000) ='group1,group10,group8'

set @groups = 'rmc.' + replace(@groups, ',', ',rmc.')
select @groups

Result:

rmc.group1,rmc.group10,rmc.group8

Problem

I am passing in a string into a stored procedure to be used in a select statement using dynamic sql: ``` @groups as nvarchar(1000) = 'group1,group10,group8' ``` I might just pass in string of numbers, eg, '1,2,3,4' I want to split these values and then concatenate them so that they end up in the following manner : ``` 'rmc.group1,rmc.group10,rmc.group8' ```

Original source