I have an SQL Server database where there is a cell with a delimiter (\
) separated string. An example of this string would be:
category_path
=============
RootCategory\Middle Category\Child Category\Child of child category
RootCategory\Middle Category\Other Child Category\
There are a lot of category paths like this. I would like to parse all of them into an other table:
category_name parent_path
====================================
RootCategory null
Middle Category RootCategory
Child Category RootCategory\Middle Category
Child of child category RootCategory\Middle Category\Child Category
Other Child Category RootCategory\Middle Category
I have left out the duplicates here, but the result could contain duplicates (I will need to cursor over the rows of this table later, and I can call distinct there).
There are a lot of examples around the net where a string is simply splitted. (Here for example) I was thinking about writing a query where I split the string, cursor over the results and accumlate the parent_path in each step, but that seems to be very suboptimal.
Is there a solution where I wouldn't have to declare so many cursors for a simple string?
Best Answer
Splitting a string with a recursive CTE is usually a bad choice but this is a case where I at least would consider a solution using that.
If you used a faster string split technique you would have to preserve the order of items and rebuild
parent_category
usingfor xml
. That might still be faster than using a recursive CTE but here is the recursive stuff anyway.SQL Fiddle
MS SQL Server 2008 Schema Setup:
Query 1:
Results: