More hierarchyid Methods

Three more hierarchyid methods—Parse, Read, and Write—are provided by the hierarchyid data type, although they are less often used.

Parse is essentially the reverse of ToString. It accepts the same slash-delimited string representation that is returned by ToString and returns the equivalent compacted varbinary hierarchyid value that SQL Server uses internally to represent the nodes. This value could then be passed to ToString, in which case you’d get the original slash-delimited string back. It is the only other static method besides GetRoot, and so it uses the double-colon syntax, as follows:

SELECT hierarchyid::Parse('/2/1/1/') AS NodeId
GO

NodeId
----------
0x6AD6

(1 row(s) affected)

Together, Parse and ToString enable ...

Get Programming Microsoft® SQL Server® 2012 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.