SELECT A.HieracrchyId, A.HierarchyId.ToString()
FROM dbo.TableA AS A
LEFT OUTER JOIN dbo.TableA AS B
ON A.HierarchyId = B.HierarchyId.GetAncestor(1)
WHERE B.HierarchyId IS NULL;
SELECT A.HieracrchyId, A.HierarchyId.ToString()
FROM dbo.TableA AS A
LEFT OUTER JOIN dbo.TableA AS B
ON A.HierarchyId = B.HierarchyId.GetAncestor(1)
WHERE B.HierarchyId IS NULL;