Good day,
The requirement is not fully clear since in the description you say "The goal is to have just names and not XX.
" but in the requested result you don't have the text "Article 3" which exists in the source.
If you simply want to remove the ", XX" then we can use simple REPLACE like bellow:
WITH WriterTbl AS (
SELECT 'Sabao Fulano, XX, Sapato Feio, XX, Jose Perreira, XX' AS Writer UNION ALL
SELECT 'Toze Jose, XX' UNION ALL
SELECT 'Feijao Mauricio, XX Article 3'
)
select REPLACE(WriterTbl.Writer,', XX',', ')
from WriterTbl
But if you actually want to remove the parts that start with ", XX" and you requirement is to use STRING_SPLIT as you asked in the original question (assuming you will not change the question), then we can use STRING_SPLIT to find the parts of the text we need to remove and than we can use the function STRING_AGG in order to concatenate the text again
There is one important point which you must remember and I am pretty sure that most people do not think about it: STRING_SPLIT does not GUARANTEED the order of the result, which means that by splitting the text and re-concatenate it, you might get different order
According to these limitations and your requirements, please check this solution:
WITH WriterTbl AS (
SELECT 'Sabao Fulano, XX, Sapato Feio, XX, Jose Perreira, XX' AS Writer UNION ALL
SELECT 'Toze Jose, XX' UNION ALL
SELECT 'Feijao Mauricio, XX Article 3'
),
MyCTE AS(
select MyGroup = ROW_NUMBER() OVER (order by (select null)), t1.Writer
from WriterTbl t1
)
SELECT STRING_AGG(t2.[value], ',')
FROM MyCTE t1
CROSS APPLY (
SELECT * from STRING_SPLIT (t1.Writer, ',') t2
) t2
where not t2.[value] like ' XX%'
group by MyGroup
Note! this task is NOT recommended, and I highly recommend you to re-think about your requirements and your architecture!
Sub-Note! Even if you stay with the need to get this result from this input, then you should probably create your own function and not use the built-in function STRING_SPLIT. There is no reason to split the data in order to concatenate it back. In your User Function you can simple remove the the text that start with ", XX" and close with "," or if this is the last part of the value. The solution here is according to your requirement to use STRING_SPLIT