Query Comma Seperated List using SQL XML
Using SQL XML query on SQL Server database, developers can search for specific values in a comma seperated list stored in a database table column.
Instead of splitting string using SQL split functions, either using string_split or user-defined SQL split function, SQL database developer can query concatenated string value using SQL XML.
Although there are certainly other solutions better in performance, this SQL tutorial shows how comma seperated string value lists (concatenated strings seperated with comma, etc) can be converted into XML data type and queried with SQL Server XML functions.
Assume that in your SQL database you create a table to store authors of each book.
The identity column values (or Primary Keys) of authors will be kept in AuthorIdList in a concatenated way so that if there are more than one author, the co-authors will be seperated by "," comma from each other.
Here is a part of my data model for this SQL requirement on SQL Server. Please ignore that I did not share Authors table here. We will not require such details for this tutorial.
Now let's populate Books table with some test data.
Now let's assume that you are looking for the list of books that a specific author has written.
Let's query for author id 2.
Please note that below query is not a correct way of searching for an ID in a comma seperated or concatenated list of values.
A filtering error has occured because the output of the above SQL query listed 12, 21 and 23 as well as targeted value 2.
Let's modify our query to make it more wise by adding commas around the target author's id value.
Nice above query worked successfully for Id 2, but what would it result if we search for author id equal to 1.
Let's execute the same SQL query for 1
The SQL query did not bring any row data. Above database query failed for first row because the list starts with "1" not with comma as we searched for ",1,".
In fact the solution is easy in this case. Let's add comma characters to the start and end of the Id list column as in following SQL Select query
I know, this works successfully with expected result set. But what about performance?
Using a column in a function like we did above will prevent use of index losing from performance.
But it still can be used for most cases.
Let's split the concatenated string value using string_split SQL function introduced with SQL Server 2016 for database programmers as follows.
As seen in above query, database table data is selected then using CROSS APPLY the table-valued function string_split returns one row for each splitted Id value. The filtering criteria in WHERE clause is directly the searched author's Id value.
Of course this method, splitting comma seperated list can be applied to the same solution by using a custom SQL split function.
Now, I want to introduce another method to solve this SQL problem.
Following SQL CONVERT function used with REPLACE procudes a column with XML data type
This XML field can be splitted into its nodes as follows
In fact, this SQL XML query does not differ from splitting string using SQL functions.
On the other hand, below SQL XML query where data is filtered with SQL Server XML functions like EXIST() is more advanced