Q: Get multiple values with brackets from rows in SQL Server I have rows containing data like this in column called ERROR_CODE:
00111[2003] Maschine0; 000222[2003] Maschinen2

I need to filter out only values in the brackets like this in one row:
2003;2003
I have one solution but only to get first element. And I would need all of them...like 2003,2003
SUBSTRING(ERROR_CODE,CHARINDEX('[',ERROR_CODE)+1 ,CHARINDEX(']',ERROR_CODE)-CHARINDEX('[',ERROR_CODE)-1)

Could you pease help me to find a solution?

A: This is based on several assumptions:

*

*Each error is semicolon (;) delimited

*An error always contains one value in brackets ([])

*You are using a fully supported version of SQL Server.

One method to achieve this would be to string your string on the delimiter (;). Then you can find the position left bracket ([) and the right (]) and SUBSTRING to get the content between. Thing finally you can string aggregate to get 1 row (per value of your column) again:
SELECT STRING_AGG(SUBSTRING(V.YourColumn,CI.LB +1, CI.RB - CI.LB - 1),',')
FROM (VALUES('00111[2003] Maschine0; 000222[2003] Maschinen2'))V(YourColumn)
     CROSS APPLY STRING_SPLIT(V.YourColumn,';') SS
     CROSS APPLY (VALUES(CHARINDEX('[',V.YourColumn),CHARINDEX(']',V.YourColumn)))CI(LB,RB)
GROUP BY V.YourColumn;

For point 3, if you are not using a fully supported version of SQL Server you will need to use a user defined (set based or CLR) string splitter and FOR XML PATH respectively for splitting and aggregating your strings. If either 1 and 2 are not true, you have a far more fundamental problem with your design that your let on; fix your design.
