Split String using XML - Convert or Split a Delimited String into Rows using SQL XML
SQL developers frequently need to parse or split delimited string values or parameters into a data rows on every database system just as on SQL Server.
Using T-SQL, sql developers or SQL Server database administrators develop different user defined functions UDF to handle string splitting task.
I guess, you have prepared such SQL UDF functions to split and convert comma delimited varchar string values into a table row columns.
With XML enhancements introduced to T-SQL developers and database administrators by SQL Server 2005, programmers are frequently using FOR XML PATH() concatenation method.
Just as concatenation, the opposite task splitting and parsing can also be implemented by using the SQL XML improvements in T-SQL.
Here I will try to demonstrate a sample split sql code which parses comma delimited string inline.
This method does not require a user defined function.
So this is somehow a little bit more advanced than the regular parsing methods we used to code during our T-SQL splitting tasks.
Of course with SQL Server 2016, database developers can use built-in SQL split string function STRING_SPLIT
Split String using XML T-SQL Example
Here is an SQL example code to split delimited string using XML and XML parsing into table rows.
The SELECT statement inside REPLACE() SQL function enables developers to escape special characters like "&" used inside the input delimited string. Otherwise SQL engine will throw an XML parsing error similar to:
XML parsing: line 1, character 30, illegal name character
In addition to the delimited string, it is a best practise to escape possible special characters within the delimiter string or delimiter character. So the @xml value assignment code will be as follows:
The syntax used as "as [*]" is for removing the column name. In above syntax removing the "as [*]" part will not make a difference, but in some cases where you use with Cross Apply it will cause a SQL error.
SQL Server database developer can still develop a T-SQL user defined function for string split to use the above code in their future parsing requirements.
In this sql split script with XML, the delimiter character can be defined in a more flexible way.
I hope, you are impressed with the T-SQL trick here. I liked split string using XML parse method much and I have already used string splitting using XML method in our SQL developments on production environments.
A Real Life Problem : An Other Example to Split String using XML
In the below sql problem, I believe sql developers or database administrators frequently experience, I will try to develop a select statement which will split values of a delimited character column value in a table and get the detail information by joining the ID values that we have just splitted.
In this SQL split string example, database table Books has a column Authors which has the id's list as concatenated by comma "," character.
After string split by XML, we will use the each Id splitted to join the BookAuthors table on AuthorId column.
If you execute the SQL Select statements on the Query Editor window, the output of the SQL query will be as follows
As seen in above SQL query, concatenated string data in database table columns can be splitted using the sample split string function and Cross Apply. The output of the query is as follows.
In this T-SQL split sample code, SQL programmers can see how easy is to use the XML in split string processes.
Cross Apply enables the sql split function which uses XML, to join on string column on the main sql table and return a table which can be joined.
The same split string query can be implemented inline as shown in the below query.
If SQL Server database programmers compare the output of the two above queries where in the first one a SQL split string function is used and in the other an inline split string SQL is used, it is seen that both outputs are exactly the same.
But if we examine the Include Actual Execution Plan for both string split methods on SQL Server, executing a string split function using XML or directly inline splitting string using XML, I see that using a user-defined function UDF which splits string using XML is better to use by comparing each Query Cost.
I will also suggest database developers to check SQL Server Recursive T-SQL Split Function where SQL recursive CTE expression is used for solving string split problems as an alternative solution.
On your SQL Server database platform, if you are already using CLR functions or procedures, then as a database developer you can also use SQL CLR Function to split string