I am looking to return a query that shows exactly like this:
Root 1
–> Child 2
—–>Child 3
Root 2
–> Child 4
—->Child 5
So the query should return Root 1 as One row, —> Child 2 as another row. Assume n Levels, and “—>” format is placed for each child. Level is higher then “—->” increases.
My Table definition is
[NodeId, ParentId, Name, Level]
On SQL Server 2008 and above, you can use hierarchyId datatype to quickly achieve the desired sorting. You can use REPLICATE() to get the dashes.
SQL Fiddle
On earlier SQL Server 2005, you can emulate the hierarchyId sorting using zero-padded strings: