Sort using Hierarchyid of SQL Server 2008

Posted by in Sql Server category on for Beginner level | Points: 250 | Views : 2942 red flag
Rating: 3 out of 5  
 1 vote(s)

This article will basically focus on sorting of hierarchical data using the Hierarchyid of SQL Server 2008

 Download source code for Sort using Hierarchyid of SQL Server 2008


SQL Server 2008, introduced the hierarchyid. This is basically use for storing the hierarchical data.Though it's been a long time that it got introduced, we found a situation recently where we felt the use of this feature and in this article, we will share the experience of that.


Suppose we have the below table

--Input Script
--Declare a sample table variable
DECLARE @tblAssemblyVersion TABLE(Version VARCHAR(20))

--Insert some records to it
INSERT INTO @tblAssemblyVersion VALUES

--Display that
SELECT * FROM @tblAssemblyVersion


And we have been asked to sort the assemblies.So how can we do that?

The more general problem statment

A normal "OrderBy" clause won't work here.If we apply that, we will receive the below output

FROM @tblAssemblyVersion


As can be figure out that "" came before "" which is not desirous.The same problem between "" and "" The question why such a problem arise? Well , it is a typical problem of "don't store delimited values in columns". A good amount of discussion regarding this has been done here

Using the code

Among the other options lets go ahead with the Hierarchyid. If you properly note, we have not created the Column type as hierarchyid. However.However,we will perform the sorting based on that.The code segment for the same is as under

SELECT * FROM @tblAssemblyVersion
ORDER BY CAST('/'+REPLACE(Version,'.','/')+'/' AS hierarchyID)


So we receive the require output that we have asked for. So what's the magic behind?. Well, the hierarchyid data type values represents a position in a tree hierarchy and it performs Depth-first search of the tree traversal.That indicates that for any two hierarchyid values e.g. x and y, if x and y are in a relationship of x<y, then in the case of Depth-first search, x will always appear before y. And hence, we got the correct answer.


a)Using HierarchyID system data type in SQL Server 2008

b)Hierarchical Data


Hope you will find this useful at some point of time.Try this out in a similar kind of situation where you find the need of a Hierarchical Data traversal using Depth-first search. Find attached code for the experiment herewith in zipped format for your reference.Thanks for reading the article.

Page copy protected against web site content infringement by Copyscape

About the Author

Full Name: Niladri Biswas (RNA Team)
Member Level: Platinum
Member Status: Member,Microsoft_MVP,MVP
Member Since: 3/17/2015 2:41:06 AM
Country: India
-- Thanks & Regards, RNA Team

Login to vote for this post.

Comments or Responses

Posted by: Kdkrishan on: 3/18/2017 | Points: 25
Can we also sort this.

Login to post response

Comment using Facebook(Author doesn't get notification)