Skip to content
Advertisement

Extract comma separated values from comma separated GUIDs

I have a column in table T1 named Categories, which contains GUIDs in XML. I am able to extract the GUIDs in a comma-separated form using the below query.

I have another table T2, which contains the names of the Categories. I now want to use these comma-separated GUIDs to go and fetch their corresponding Name from T2.

What changes do I need to make in my SELECT statement to write a LEFT OUTER JOIN which takes this comma-separated GUIDs and returns comma-separated names from T2.

T2 looks something like this:

T2 looks something like this

Advertisement

Answer

I would join the category name table before concatenating the values to avoid another iteration of splitting and concatenating.

Sample data

Solution

Two versions because the SQL Server version is not specified in the question tags… The string_agg() function is available starting from SQL Server 2017.

With string_agg()

Without string_agg()

Result

Fiddle to see things in action.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement