{"id":241741,"date":"2023-06-27T14:20:09","date_gmt":"2023-06-27T21:20:09","guid":{"rendered":"https:\/\/virtual-dba.com\/?p=241741"},"modified":"2023-06-29T18:37:50","modified_gmt":"2023-06-30T01:37:50","slug":"how-to-use-hierarchyid-data-type-in-sql-server","status":"publish","type":"post","link":"https:\/\/virtual-dba.com\/blog\/how-to-use-hierarchyid-data-type-in-sql-server\/","title":{"rendered":"How to Use HierarchyID Data Type in SQL Server"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">What is the HierarchyID Data Type?<\/h2>\n\n\n\n<p>Hierarchical data is a common type of data that represents the relationships between entities in a tree-like structure. For example, you may have hierarchical data for your organizational structure, where each employee has a manager and a department; or for your file system, where each folder can contain subfolders and files; or for your website, where each page can have subpages and links.<\/p>\n\n\n\n<p>Storing and querying hierarchical data can be challenging in a relational database, where the data is organized in tables with rows and columns. You may need to use complex joins or recursive queries to traverse the hierarchy or find the ancestors or descendants of a given node. You may also need to use additional columns or tables to store the level or order of the nodes in the hierarchy.<\/p>\n\n\n\n<p>SQL Server (2008 and later) provides a solution for these challenges with the <strong>HierarchyID<\/strong> data type. The HierarchyID data type is a variable-length system data type that represents a position in a hierarchy. It has a special way of representing the relationships between the nodes in a hierarchy from top to bottom levels and from left to right among the children nodes of a parent node.<\/p>\n\n\n\n<p>The HierarchyID data type uses a compact binary format that encodes the path from the root node to any node in the hierarchy. For example, the root node has a HierarchyID value of <code>\/<\/code>, while its first child has a value of <code>\/1\/<\/code>, and its second child has a value of <code>\/2\/<\/code>. The first child of the first child has a value of <code>\/1\/1\/<\/code>, and so on. This format allows you to compare and sort HierarchyID values easily using simple operators such as <code>&lt;<\/code>, <code>&gt;<\/code>, =, or <code>&lt;&gt;<\/code>.<\/p>\n\n\n\n<p>The HierarchyID data type also provides several methods that allow you to perform common hierarchical operations on HierarchyID values.<\/p>\n\n\n\n<p>For example, you can use the <code>GetAncestor()<\/code> method to find the parent node of a given node; or the <code>GetDescendant()<\/code> method to find the next child node of a given node; or the <code>IsDescendantOf()<\/code> method to check if one node is a descendant of another node.<\/p>\n\n\n\n<p>Let&#8217;s say we have a table called Employees that stores the hierarchy of employees in an organization.&nbsp;<\/p>\n\n\n\n<p>The table has the following columns:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Id:<\/strong> int, the primary key of the table<\/li>\n\n\n\n<li><strong>Name:<\/strong> varchar(50), the name of the employee<\/li>\n\n\n\n<li><strong>Position: <\/strong>hierarchyid, the position of the employee in the hierarchy<\/li>\n\n\n\n<li><strong>Level:<\/strong> as computed, the level of the employee in the hierarchy<\/li>\n<\/ul>\n\n\n\n<p>The table has the following data:<\/p>\n\n\n<table>\n<tbody>\n<tr>\n<td>\n<p><b>Id<\/b><\/p>\n<\/td>\n<td>\n<p><b>Name<\/b><\/p>\n<\/td>\n<td>\n<p><b>Position<\/b><\/p>\n<\/td>\n<td>\n<p><b>Level<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p><span style=\"font-weight: 400;\">1<\/span><\/p>\n<\/td>\n<td>\n<p><span style=\"font-weight: 400;\">Alice<\/span><\/p>\n<\/td>\n<td>\n<p><span style=\"font-weight: 400;\">\/<\/span><\/p>\n<\/td>\n<td>\n<p><span style=\"font-weight: 400;\">0<\/span><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p><span style=\"font-weight: 400;\">2<\/span><\/p>\n<\/td>\n<td>\n<p><span style=\"font-weight: 400;\">Bob<\/span><\/p>\n<\/td>\n<td>\n<p><span style=\"font-weight: 400;\">\/1\/<\/span><\/p>\n<\/td>\n<td>\n<p><span style=\"font-weight: 400;\">1<\/span><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p><span style=\"font-weight: 400;\">3<\/span><\/p>\n<\/td>\n<td>\n<p><span style=\"font-weight: 400;\">Carol<\/span><\/p>\n<\/td>\n<td>\n<p><span style=\"font-weight: 400;\">\/2\/<\/span><\/p>\n<\/td>\n<td>\n<p><span style=\"font-weight: 400;\">1<\/span><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p><span style=\"font-weight: 400;\">4<\/span><\/p>\n<\/td>\n<td>\n<p><span style=\"font-weight: 400;\">Dave<\/span><\/p>\n<\/td>\n<td>\n<p><span style=\"font-weight: 400;\">\/1\/1\/<\/span><\/p>\n<\/td>\n<td>\n<p><span style=\"font-weight: 400;\">2<\/span><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p><span style=\"font-weight: 400;\">5<\/span><\/p>\n<\/td>\n<td>\n<p><span style=\"font-weight: 400;\">Eve<\/span><\/p>\n<\/td>\n<td>\n<p><span style=\"font-weight: 400;\">\/1\/2\/<\/span><\/p>\n<\/td>\n<td>\n<p><span style=\"font-weight: 400;\">2<\/span><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p><span style=\"font-weight: 400;\">6<\/span><\/p>\n<\/td>\n<td>\n<p><span style=\"font-weight: 400;\">Frank<\/span><\/p>\n<\/td>\n<td>\n<p><span style=\"font-weight: 400;\">\/2\/1\/<\/span><\/p>\n<\/td>\n<td>\n<p><span style=\"font-weight: 400;\">2<\/span><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p><span style=\"font-weight: 400;\">7<\/span><\/p>\n<\/td>\n<td>\n<p><span style=\"font-weight: 400;\">Grace<\/span><\/p>\n<\/td>\n<td>\n<p><span style=\"font-weight: 400;\">\/1\/1\/1\/<\/span><\/p>\n<\/td>\n<td>\n<p><span style=\"font-weight: 400;\">3<\/span><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p><span style=\"font-weight: 400;\">8<\/span><\/p>\n<\/td>\n<td>\n<p><span style=\"font-weight: 400;\">Harry<\/span><\/p>\n<\/td>\n<td>\n<p><span style=\"font-weight: 400;\">\/2\/1\/1\/<\/span><\/p>\n<\/td>\n<td>\n<p><span style=\"font-weight: 400;\">3<\/span><\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"font-weight: 400;\">The hierarchy looks like this:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Alice<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u251c\u2500\u2500 Bob<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u2502 \u00a0 \u251c\u2500\u2500 Dave<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u2502 \u00a0 \u2502 \u00a0 \u2514\u2500\u2500 Grace<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u2502 \u00a0 \u2514\u2500\u2500 Eve<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u2514\u2500\u2500 Carol<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u2514\u2500\u2500 Frank<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u2514\u2500\u2500 Harry<\/span><\/p>\n\n\n<h2 class=\"wp-block-heading\">Querying Hierarchical Data Using the HierarchyID Data Type<\/h2>\n\n\n\n<p>The HierarchyID data type provides several methods that allow you to perform common hierarchical operations on HierarchyID values.<\/p>\n\n\n\n<p>To find the parent node of a given node, you can use the <strong>GetAncestor()<\/strong> method with an argument of 1. For example, if you want to find the parent node of Eve, you can use GetAncestor (1) to return `\/1\/`, which is Bob&#8217;s position.\u00a0<\/p>\n\n\n\n<p>The code would look like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>DECLARE @CurrentNode hierarchyid;\nSELECT @CurrentNode = Position FROM Employees WHERE Name = 'Eve';\nSELECT Name FROM Employees WHERE Position = @CurrentNode.GetAncestor(1); \n-- returns Bob<\/code><\/pre>\n\n\n\n<p>To find the next child node of a given node, you can use the <strong>GetDescendant()<\/strong> method with two arguments that specify the left and right boundaries of the child node. For example, if you want to find the next child node of Bob, you can use GetDescendant (NULL, NULL) to return `\/1\/3\/`, which is a new position that does not exist yet, or GetDescendant (&#8216;\/1\/2\/&#8217;, NULL) to return `\/1\/3\/`, which is also a new position.&nbsp;<\/p>\n\n\n\n<p>The code would look like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>DECLARE @CurrentNode hierarchyid;\nSELECT @CurrentNode = Position FROM Employees WHERE Name = 'Bob';\nSELECT @CurrentNode.GetDescendant(NULL, NULL); -- returns \/1\/3\/\nSELECT @CurrentNode.GetDescendant('\/1\/2\/', NULL); -- returns \/1\/3\/<\/code><\/pre>\n\n\n\n<p>To check if one node is a descendant of another node, you can use the <strong>IsDescendantOf()<\/strong> method with an argument that specifies the parent node. For example, if you want to check if Harry is a descendant of Alice, you can use IsDescendantOf (&#8216;\/&#8217;) to return 1 (true), which means that Harry is a descendant of Alice; or IsDescendantOf (&#8216;\/2\/&#8217;) to return 0 (false), which means that Harry is not a descendant of Carol.&nbsp;<\/p>\n\n\n\n<p>The code would look like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>DECLARE @ChildNode hierarchyid;\nDECLARE @ParentNode hierarchyid;\nSELECT @ChildNode = Position FROM Employees WHERE Name = 'Harry';\nSELECT @ParentNode = '\/';\nSELECT @ChildNode.IsDescendantOf(@ParentNode); -- returns 1 (true)\nSELECT @ParentNode = '\/2\/';\nSELECT @ChildNode.IsDescendantOf(@ParentNode); -- returns 0 (false)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Examples of \u201cHierarchical Data\u201d<\/h2>\n\n\n\n<p>Data that would benefit from the HierarchyID data type is more common than you might think. Here are some real-world examples of hierarchy structures that could potentially be in your SQL Server database:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Organizational structure<\/strong>: You can use HierarchyID to store the reporting relationships between employees in your organization. You can also use it to query the hierarchy for various purposes, such as finding the direct reports of a manager, finding the managers of an employee, finding the employees in a department, etc.<\/li>\n\n\n\n<li><strong>Folders, subfolders, and files<\/strong>: You can use HierarchyID to store the structure of your file system. You can also use it to query the hierarchy for various purposes, such as finding the files in a folder, finding the subfolders of a folder, finding the path of a file, etc.<\/li>\n\n\n\n<li><strong>Tasks and subtasks in a project<\/strong>: You can use HierarchyID to store the breakdown of tasks and subtasks in your project management system. You can also use it to query the hierarchy for various purposes, such as finding the progress of a task, finding the dependencies of a task, finding the subtasks of a task, etc.<\/li>\n\n\n\n<li><strong>Pages and subpages of a website<\/strong>: You can use HierarchyID to store the structure of your website. You can also use it to query the hierarchy for various purposes, such as finding the subpages of a page, finding the links of a page, finding the breadcrumbs of a page, etc.<\/li>\n\n\n\n<li><strong>Geographical data with countries, regions, and cities:<\/strong> You can use HierarchyID to store the hierarchy of geographical entities such as countries, regions, and cities. You can also use it to query the hierarchy for various purposes, such as finding the countries in a region, finding the regions in a country, finding the cities in a region, etc.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">When Should I Use the HierarchyID Data Type?<\/h2>\n\n\n\n<p>You should use HierarchyID when\u2026<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>You have hierarchical data that you want to store in a database table.<\/li>\n\n\n\n<li>You want to perform hierarchical operations such as traversing the tree structure or finding the parent or child nodes of a given node.<\/li>\n\n\n\n<li>You want to store hierarchical data in a compact format that is easy to query.<\/li>\n<\/ul>\n\n\n\n<p>You should NOT use HierarchyID when\u2026<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>You have non-hierarchical data that does not fit into a tree structure.<\/li>\n\n\n\n<li>You have very large trees with many levels or nodes. In this case, it may be more efficient to use other methods, such as nested sets.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>In conclusion, the HierarchyID data type is a useful feature in SQL Server that can help you store and query hierarchical data efficiently and easily.<\/p>\n\n\n\n<p>For more information, please <a href=\"https:\/\/virtual-dba.com\/contact-us\/\">contact us!<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is the HierarchyID Data Type? Hierarchical data is a common type of data that represents the relationships between entities in a tree-like structure. For example, you may have hierarchical data for your organizational structure, where each employee has a manager and a department; or for your file system, where each folder can contain subfolders [&hellip;]<\/p>\n","protected":false},"author":59,"featured_media":241745,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"off","_et_pb_old_content":"","_et_gb_content_width":"","content-type":"","footnotes":""},"categories":[4166,55],"tags":[4086,60],"class_list":["post-241741","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog","category-sql-server","tag-databases","tag-sql-server"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.1 (Yoast SEO v27.1.1) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>How to Use HierarchyID Data Type in SQL Server - VDBA<\/title>\n<meta name=\"description\" content=\"Learn how to use the HierarchyID data type in SQL Server to store and query hierarchical data efficiently and easily.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/virtual-dba.com\/blog\/how-to-use-hierarchyid-data-type-in-sql-server\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Use HierarchyID Data Type in SQL Server\" \/>\n<meta property=\"og:description\" content=\"Learn how to use the HierarchyID data type in SQL Server to store and query hierarchical data efficiently and easily.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/virtual-dba.com\/blog\/how-to-use-hierarchyid-data-type-in-sql-server\/\" \/>\n<meta property=\"og:site_name\" content=\"Virtual-DBA Remote DBA Services &amp; Support - Certified Database Experts\" \/>\n<meta property=\"article:published_time\" content=\"2023-06-27T21:20:09+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-06-30T01:37:50+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/virtual-dba.com\/wp-content\/uploads\/How-to-Use-HierarchyID-Data-Type-in-SQL-Server.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"557\" \/>\n\t<meta property=\"og:image:height\" content=\"291\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Ken Haff\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@virtual_dba\" \/>\n<meta name=\"twitter:site\" content=\"@virtual_dba\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ken Haff\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/virtual-dba.com\/blog\/how-to-use-hierarchyid-data-type-in-sql-server\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/virtual-dba.com\/blog\/how-to-use-hierarchyid-data-type-in-sql-server\/\"},\"author\":{\"name\":\"Ken Haff\",\"@id\":\"https:\/\/virtual-dba.com\/#\/schema\/person\/8921c8551455c88e6da338f28f7db365\"},\"headline\":\"How to Use HierarchyID Data Type in SQL Server\",\"datePublished\":\"2023-06-27T21:20:09+00:00\",\"dateModified\":\"2023-06-30T01:37:50+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/virtual-dba.com\/blog\/how-to-use-hierarchyid-data-type-in-sql-server\/\"},\"wordCount\":1115,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/virtual-dba.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/virtual-dba.com\/blog\/how-to-use-hierarchyid-data-type-in-sql-server\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/virtual-dba.com\/wp-content\/uploads\/How-to-Use-HierarchyID-Data-Type-in-SQL-Server.jpg\",\"keywords\":[\"databases\",\"sql server\"],\"articleSection\":[\"Blog\",\"SQL Server\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/virtual-dba.com\/blog\/how-to-use-hierarchyid-data-type-in-sql-server\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/virtual-dba.com\/blog\/how-to-use-hierarchyid-data-type-in-sql-server\/\",\"url\":\"https:\/\/virtual-dba.com\/blog\/how-to-use-hierarchyid-data-type-in-sql-server\/\",\"name\":\"How to Use HierarchyID Data Type in SQL Server - VDBA\",\"isPartOf\":{\"@id\":\"https:\/\/virtual-dba.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/virtual-dba.com\/blog\/how-to-use-hierarchyid-data-type-in-sql-server\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/virtual-dba.com\/blog\/how-to-use-hierarchyid-data-type-in-sql-server\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/virtual-dba.com\/wp-content\/uploads\/How-to-Use-HierarchyID-Data-Type-in-SQL-Server.jpg\",\"datePublished\":\"2023-06-27T21:20:09+00:00\",\"dateModified\":\"2023-06-30T01:37:50+00:00\",\"description\":\"Learn how to use the HierarchyID data type in SQL Server to store and query hierarchical data efficiently and easily.\",\"breadcrumb\":{\"@id\":\"https:\/\/virtual-dba.com\/blog\/how-to-use-hierarchyid-data-type-in-sql-server\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/virtual-dba.com\/blog\/how-to-use-hierarchyid-data-type-in-sql-server\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/virtual-dba.com\/blog\/how-to-use-hierarchyid-data-type-in-sql-server\/#primaryimage\",\"url\":\"https:\/\/virtual-dba.com\/wp-content\/uploads\/How-to-Use-HierarchyID-Data-Type-in-SQL-Server.jpg\",\"contentUrl\":\"https:\/\/virtual-dba.com\/wp-content\/uploads\/How-to-Use-HierarchyID-Data-Type-in-SQL-Server.jpg\",\"width\":557,\"height\":291,\"caption\":\"How to Use HierarchyID Data Type in SQL Server\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/virtual-dba.com\/blog\/how-to-use-hierarchyid-data-type-in-sql-server\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/virtual-dba.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Use HierarchyID Data Type in SQL Server\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/virtual-dba.com\/#website\",\"url\":\"https:\/\/virtual-dba.com\/\",\"name\":\"Virtual-DBA Remote DBA Services &amp; Support - Certified Database Experts\",\"description\":\"Remote Database Administration\",\"publisher\":{\"@id\":\"https:\/\/virtual-dba.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/virtual-dba.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/virtual-dba.com\/#organization\",\"name\":\"Virtual-DBA: Remote DBA | Remote Database Administration\",\"alternateName\":\"Virtual-DBA powered by XTIVIA\",\"url\":\"https:\/\/virtual-dba.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/virtual-dba.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/virtual-dba.com\/wp-content\/uploads\/V-DBA-Database-Services-and-Support-Featured-Logo.jpg\",\"contentUrl\":\"https:\/\/virtual-dba.com\/wp-content\/uploads\/V-DBA-Database-Services-and-Support-Featured-Logo.jpg\",\"width\":557,\"height\":291,\"caption\":\"Virtual-DBA: Remote DBA | Remote Database Administration\"},\"image\":{\"@id\":\"https:\/\/virtual-dba.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/virtual_dba\",\"https:\/\/www.linkedin.com\/showcase\/36220649\/\",\"https:\/\/www.youtube.com\/channel\/UCx3AIeUQ2ziTLKZSJDZ-SEg\"],\"description\":\"Eliminate database downtime and spiraling costs with XTIVIA\u2019s Virtual-DBA. In today\u2019s always-on business world, gaps in 24x7 on-call DBA support, neglected maintenance and security, or a stretched team struggling with overwhelming workloads can lead to costly disruptions and threaten business continuity. XTIVIA\u2019s Virtual-DBA provides the immediate, expert database administration you need, exactly when you need it, ensuring optimal performance, ironclad security, and significant cost savings without the burden of expanding your in-house team. The goal of Virtual-DBA is to provide a cost-effective solution for organizations seeking to optimize the security, management, maintenance, availability, and performance of their critical business systems, whether self-managed or cloud-managed (e.g., AWS RDS, Azure SQL Database). We accomplish this through a comprehensive remote DBA service offering designed specifically to meet the Oracle\u00ae, DB2\u00ae, Informix\u00ae, MySQL\u2122, PostgreSQL\u00ae, MongoDB\u00ae, MariaDB, and Microsoft SQL Server\u00ae, CockroachDB, Databricks, AWS, and Azure needs of our clients.\",\"email\":\"info@xtivia.com\",\"telephone\":\"8886853101\",\"legalName\":\"XTIVIA, Inc\",\"foundingDate\":\"1992-05-01\",\"numberOfEmployees\":{\"@type\":\"QuantitativeValue\",\"minValue\":\"201\",\"maxValue\":\"500\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/virtual-dba.com\/#\/schema\/person\/8921c8551455c88e6da338f28f7db365\",\"name\":\"Ken Haff\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/virtual-dba.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/771e9bc96721a00bb741fc19620fa62a291df050a23dd669af66b0a6f7f121f5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/771e9bc96721a00bb741fc19620fa62a291df050a23dd669af66b0a6f7f121f5?s=96&d=mm&r=g\",\"caption\":\"Ken Haff\"},\"url\":\"https:\/\/virtual-dba.com\/author\/ken-haff\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"How to Use HierarchyID Data Type in SQL Server - VDBA","description":"Learn how to use the HierarchyID data type in SQL Server to store and query hierarchical data efficiently and easily.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/virtual-dba.com\/blog\/how-to-use-hierarchyid-data-type-in-sql-server\/","og_locale":"en_US","og_type":"article","og_title":"How to Use HierarchyID Data Type in SQL Server","og_description":"Learn how to use the HierarchyID data type in SQL Server to store and query hierarchical data efficiently and easily.","og_url":"https:\/\/virtual-dba.com\/blog\/how-to-use-hierarchyid-data-type-in-sql-server\/","og_site_name":"Virtual-DBA Remote DBA Services &amp; Support - Certified Database Experts","article_published_time":"2023-06-27T21:20:09+00:00","article_modified_time":"2023-06-30T01:37:50+00:00","og_image":[{"width":557,"height":291,"url":"https:\/\/virtual-dba.com\/wp-content\/uploads\/How-to-Use-HierarchyID-Data-Type-in-SQL-Server.jpg","type":"image\/jpeg"}],"author":"Ken Haff","twitter_card":"summary_large_image","twitter_creator":"@virtual_dba","twitter_site":"@virtual_dba","twitter_misc":{"Written by":"Ken Haff","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/virtual-dba.com\/blog\/how-to-use-hierarchyid-data-type-in-sql-server\/#article","isPartOf":{"@id":"https:\/\/virtual-dba.com\/blog\/how-to-use-hierarchyid-data-type-in-sql-server\/"},"author":{"name":"Ken Haff","@id":"https:\/\/virtual-dba.com\/#\/schema\/person\/8921c8551455c88e6da338f28f7db365"},"headline":"How to Use HierarchyID Data Type in SQL Server","datePublished":"2023-06-27T21:20:09+00:00","dateModified":"2023-06-30T01:37:50+00:00","mainEntityOfPage":{"@id":"https:\/\/virtual-dba.com\/blog\/how-to-use-hierarchyid-data-type-in-sql-server\/"},"wordCount":1115,"commentCount":0,"publisher":{"@id":"https:\/\/virtual-dba.com\/#organization"},"image":{"@id":"https:\/\/virtual-dba.com\/blog\/how-to-use-hierarchyid-data-type-in-sql-server\/#primaryimage"},"thumbnailUrl":"https:\/\/virtual-dba.com\/wp-content\/uploads\/How-to-Use-HierarchyID-Data-Type-in-SQL-Server.jpg","keywords":["databases","sql server"],"articleSection":["Blog","SQL Server"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/virtual-dba.com\/blog\/how-to-use-hierarchyid-data-type-in-sql-server\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/virtual-dba.com\/blog\/how-to-use-hierarchyid-data-type-in-sql-server\/","url":"https:\/\/virtual-dba.com\/blog\/how-to-use-hierarchyid-data-type-in-sql-server\/","name":"How to Use HierarchyID Data Type in SQL Server - VDBA","isPartOf":{"@id":"https:\/\/virtual-dba.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/virtual-dba.com\/blog\/how-to-use-hierarchyid-data-type-in-sql-server\/#primaryimage"},"image":{"@id":"https:\/\/virtual-dba.com\/blog\/how-to-use-hierarchyid-data-type-in-sql-server\/#primaryimage"},"thumbnailUrl":"https:\/\/virtual-dba.com\/wp-content\/uploads\/How-to-Use-HierarchyID-Data-Type-in-SQL-Server.jpg","datePublished":"2023-06-27T21:20:09+00:00","dateModified":"2023-06-30T01:37:50+00:00","description":"Learn how to use the HierarchyID data type in SQL Server to store and query hierarchical data efficiently and easily.","breadcrumb":{"@id":"https:\/\/virtual-dba.com\/blog\/how-to-use-hierarchyid-data-type-in-sql-server\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/virtual-dba.com\/blog\/how-to-use-hierarchyid-data-type-in-sql-server\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/virtual-dba.com\/blog\/how-to-use-hierarchyid-data-type-in-sql-server\/#primaryimage","url":"https:\/\/virtual-dba.com\/wp-content\/uploads\/How-to-Use-HierarchyID-Data-Type-in-SQL-Server.jpg","contentUrl":"https:\/\/virtual-dba.com\/wp-content\/uploads\/How-to-Use-HierarchyID-Data-Type-in-SQL-Server.jpg","width":557,"height":291,"caption":"How to Use HierarchyID Data Type in SQL Server"},{"@type":"BreadcrumbList","@id":"https:\/\/virtual-dba.com\/blog\/how-to-use-hierarchyid-data-type-in-sql-server\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/virtual-dba.com\/"},{"@type":"ListItem","position":2,"name":"How to Use HierarchyID Data Type in SQL Server"}]},{"@type":"WebSite","@id":"https:\/\/virtual-dba.com\/#website","url":"https:\/\/virtual-dba.com\/","name":"Virtual-DBA Remote DBA Services &amp; Support - Certified Database Experts","description":"Remote Database Administration","publisher":{"@id":"https:\/\/virtual-dba.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/virtual-dba.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/virtual-dba.com\/#organization","name":"Virtual-DBA: Remote DBA | Remote Database Administration","alternateName":"Virtual-DBA powered by XTIVIA","url":"https:\/\/virtual-dba.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/virtual-dba.com\/#\/schema\/logo\/image\/","url":"https:\/\/virtual-dba.com\/wp-content\/uploads\/V-DBA-Database-Services-and-Support-Featured-Logo.jpg","contentUrl":"https:\/\/virtual-dba.com\/wp-content\/uploads\/V-DBA-Database-Services-and-Support-Featured-Logo.jpg","width":557,"height":291,"caption":"Virtual-DBA: Remote DBA | Remote Database Administration"},"image":{"@id":"https:\/\/virtual-dba.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/virtual_dba","https:\/\/www.linkedin.com\/showcase\/36220649\/","https:\/\/www.youtube.com\/channel\/UCx3AIeUQ2ziTLKZSJDZ-SEg"],"description":"Eliminate database downtime and spiraling costs with XTIVIA\u2019s Virtual-DBA. In today\u2019s always-on business world, gaps in 24x7 on-call DBA support, neglected maintenance and security, or a stretched team struggling with overwhelming workloads can lead to costly disruptions and threaten business continuity. XTIVIA\u2019s Virtual-DBA provides the immediate, expert database administration you need, exactly when you need it, ensuring optimal performance, ironclad security, and significant cost savings without the burden of expanding your in-house team. The goal of Virtual-DBA is to provide a cost-effective solution for organizations seeking to optimize the security, management, maintenance, availability, and performance of their critical business systems, whether self-managed or cloud-managed (e.g., AWS RDS, Azure SQL Database). We accomplish this through a comprehensive remote DBA service offering designed specifically to meet the Oracle\u00ae, DB2\u00ae, Informix\u00ae, MySQL\u2122, PostgreSQL\u00ae, MongoDB\u00ae, MariaDB, and Microsoft SQL Server\u00ae, CockroachDB, Databricks, AWS, and Azure needs of our clients.","email":"info@xtivia.com","telephone":"8886853101","legalName":"XTIVIA, Inc","foundingDate":"1992-05-01","numberOfEmployees":{"@type":"QuantitativeValue","minValue":"201","maxValue":"500"}},{"@type":"Person","@id":"https:\/\/virtual-dba.com\/#\/schema\/person\/8921c8551455c88e6da338f28f7db365","name":"Ken Haff","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/virtual-dba.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/771e9bc96721a00bb741fc19620fa62a291df050a23dd669af66b0a6f7f121f5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/771e9bc96721a00bb741fc19620fa62a291df050a23dd669af66b0a6f7f121f5?s=96&d=mm&r=g","caption":"Ken Haff"},"url":"https:\/\/virtual-dba.com\/author\/ken-haff\/"}]}},"_links":{"self":[{"href":"https:\/\/virtual-dba.com\/wp-json\/wp\/v2\/posts\/241741","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/virtual-dba.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/virtual-dba.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/virtual-dba.com\/wp-json\/wp\/v2\/users\/59"}],"replies":[{"embeddable":true,"href":"https:\/\/virtual-dba.com\/wp-json\/wp\/v2\/comments?post=241741"}],"version-history":[{"count":0,"href":"https:\/\/virtual-dba.com\/wp-json\/wp\/v2\/posts\/241741\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/virtual-dba.com\/wp-json\/wp\/v2\/media\/241745"}],"wp:attachment":[{"href":"https:\/\/virtual-dba.com\/wp-json\/wp\/v2\/media?parent=241741"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/virtual-dba.com\/wp-json\/wp\/v2\/categories?post=241741"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/virtual-dba.com\/wp-json\/wp\/v2\/tags?post=241741"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}